Like other high-level languages PHP offers control structures to branch to different statements depending on whether expressions return true or false.
The two statements supported are if, with an optional else clause, and switch with two or more case clauses.
The if ... else Statement
The if statement tests whether a condition is true, and if so, executes one or more statements.
Example of a Simple if Statement
<?php
$var = 12;
if ($var < 20)
echo("This variable is less than 20");
?>
If more than one statement is to be executed then they must be surrounded by braces.
if Statement with Braces
<?php
$var = 12;
if ($var < 20)
{
echo("This variable is less than 20");
echo("Or even less than twenty");
}
?>
The optional else clause is used to execute a statement or group of statements if the expression evaluates as false.
if ... else Statement
<?php
$var = 12;
if ($var < 20)
{
echo("This variable is less than 20");
}
else
{
echo("This variable is equal to or greater than 20");
}
?>
In the example above the braces were optional but with more than one statement they are required.
If a series of conditions need to be tested then if ...else can be nested or the elseif can be used but this can lead to long-winded unreadable code. A better alternative is the switch statement.
The Switch Statement
The expression to be tested goes within the switch statement and each of the possible values that the expression can take is listed as a case followed by the statement(s) to be executed if the case is true.
Example of the Switch Statement
<?php
switch ($var)
{
case 1:
echo("The variable equals 1");
break;
case 2:
echo("The variable equals 2");
break;
case 3:
echo("The variable equals 3");
break;
default:
echo("The variable doesn't equal 1, 2 or 3");
}
?>
There are two things to remember about the switch statement.
Each case must end with break otherwise whatever case statements follow the one picked will be executed as well the required one.
A default case should be included to cover any case not mentioned or the construct will "fall through" to the statements following the switch construct.
Conditional Expressions
A number of conditions can be tested for within an if statement.
if ($var == expression) true if $var equals expression.
if ($var > expression) true if $var is greater than expression.
if ($var < expression) true if $var is less than expression.
if ($var >= expression) true if $var is greater than or equal to expression.
if ($var <= expression) true if $var is less than or equal to expression.
if ($var != expression) true if $var is not equal to expression.
Note: A common mistake is to use the = assignment operator in place of the == equality operator which always results in true.
Using Boolean Operators
Expressions can be combined using parentheses and used with the Boolean operators && (and) and || (or).
&& will return true only if both expressions returns true.
|| will return true if either expression returns true.
Examples of Boolean Operators
<?php
//Will output only if both conditions are true
if ($name == "Fred" && $age == 40)
{
echo ("Congratulations on being 40 Fred.");
}
//Will output if either condition is true
if ($name == "Fred" || $name == "George")
{
echo ("You're either Fred or George.");
}
?>
Multiple expressions can be used with combinations of Boolean operators and the use of parentheses although the larger they are the more tortured the logic can become.
The is-identical Operator
This is unique to PHP and was introduced with PHP4. It's denoted by === and only returns true if the expressions evaluate as equal and the arguments are of the same type.
For example if $var_1 = 10 and $var_2 = 10.0 are compared then they will not return true since one is an integer and the other a float.
The not Operator
Any Boolean expression can be negated with an exclamation mark!