Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
Quick Table of Contents
[Edit] if Statement
The easiest decision - making statement to understand is the if statement. The basic form of an if
construct is as follows:
if ( expression ) {
// Run this code
}
// More code here
If the expression inside the parentheses evaluates to true , the code between the braces is run. If the expression evaluates to false , the code between the braces is skipped. That ’ s really all there is to it. It’s worth pointing out that any code following the closing brace is always run, regardless of the result of the test. So in the preceding example, if expression evaluates to true , both the Run this code and More code here lines are executed; if expression evaluates to false , Run this code is skipped but More code here is still run. Here ’ s a simple real - world example:
$widgets = 23;
if ( $widgets == 23 ) {
echo "We have exactly 23 widgets in stock!";
}
The first line of the script creates a variable, $widgets , and sets its value to 23 . Then an if statement uses the == operator to check if the value stored in $widgets does indeed equal 23 . If it does — and it should! — the expression evaluates to true and the script displays the message: " We have exactly 23 widgets in stock! " If $widgets doesn’t hold the value 23 , the code between the parentheses — that is, the echo() statement — is skipped. (You can test this for yourself by changing the value in the first line of code and re - running the example.) Here’s another example that uses the >= (greater than or equal) and <= (less than or equal) comparison operators, as well as the && (and) logical operator:
$widgets = 23;
if ( $widgets > = 10 & & $widgets < = 20 ) {
echo "We have between 10 and 20 widgets in stock.";
}
This example is similar to the previous one, but the test expression inside the parentheses is slightly more complex. If the value stored in $widgets is greater than or equal to 10 , and it ’ s also less than or equal to 20 , the expression evaluates to true and the message " We have between 10 and 20 widgets in stock. " is displayed. If either of the comparison operations evaluates to false , the overall expression also evaluates to false , the echo() statement is skipped, and nothing is displayed.

The key point to remember is that, no matter how complex your test expression is, if the whole expression evaluates to true the code inside the braces is run; otherwise the code inside the braces is skipped and execution continues with the first line of code after the closing brace.

You can have as many lines of code between the braces as you like, and the code can do anything, such as display something in the browser, call a function, or even exit the script. In fact, here ’ s the previous example rewritten to use an if statement inside another if statement:
$widgets = 23;
if ( $widgets > = 10 ) {
if ( $widgets < = 20 ) {
echo "We have between 10 and 20 widgets in stock.";
}
}
The code block between the braces of the first if statement is itself another if statement. The first if statement runs its code block if $widgets > = 10 , whereas the inner if statement runs its code block — the echo() statement — if $widgets < = 20 . Because both if expressions need to evaluate to true for the echo() statement to run, the end result is the same as the previous example.

If you only have one line of code between the braces you can, in fact, omit the braces altogether:

$widgets = 23;
if ( $widgets == 23 )
echo "We have exactly 23 widgets in stock!";
However, if you do this, take care to add braces if you later add additional lines of code to the code
block. Otherwise, your code will not run as expected!
[Edit] else Statement
As you’ve seen, the if statement allows you to run a block of code if an expression evaluates to true . If

the expression evaluates to false , the code is skipped.
You can enhance this decision - making process by adding an else statement to an if construction. This
lets you run one block of code if an expression is true , and a different block of code if the expression is
false . For example:

if ( $widgets > = 10 ) {
echo "We have plenty of widgets in stock.";
} else {
echo "Less than 10 widgets left. Time to order some more!";
}
If $widgets is greater than or equal to 10 , the first code block is run, and the message " We have plenty of widgets in stock. " is displayed. However, if $widgets is less than 10 , the second code block is run, and the visitor sees the message: " Less than 10 widgets left. Time to order some more! "

You can even combine the else statement with another if statement to make as many alternative choices as you like:

if ( $widgets > = 10 ) {
echo "We have plenty of widgets in stock.";
} else if ( $widgets > = 5 ) {
echo "Less than 10 widgets left. Time to order some more!";
} else {
echo "Panic stations: Less than 5 widgets left! Order more now!";
}
If there are 10 or more widgets in stock, the first code block is run, displaying the message: " We have plenty of widgets in stock. " However, if
$widgets
is less than 10 , control passes to the first else statement, which in turn runs the second if statement: if ( $widgets > = 5 ) . If this is true the second message — " Less than 10 widgets left. Time to order some more! " — is displayed. However, if the result of this second if expression is false , execution passes to the final else code block, and the message " Panic stations: Less than 5 widgets left! Order more now! " is displayed. PHP even gives you a special statement — elseif — that you can use to combine an else and an if statement. So the preceding example can be rewritten as follows:
if ( $widgets > = 10 ) {
echo "We have plenty of widgets in stock.";
} elseif ( $widgets > = 5 ) {
echo "Less than 10 widgets left. Time to order some more!";
} else {
echo "Panic stations: Less than 5 widgets left! Order more now!";
}

Testing One Expression Many Times with the
switch Statement
Compact Coding with the Ternary Operator