PHP if else statement
Description
We can providing an alternative choice with the else statement.
Syntax
The PHP if else statement has the following syntax.
if(condition_0){/*from w w w . ja va 2 s . co m*/
...
}else{
...
}
or
if(condition_0){/*from ww w .j av a 2s . c o m*/
...
}elseif (condition_1){
}elseif (condition_2){
}else{
...
}
Note
else
statement lets you run one block of code
if an expression is true, and a different block of code if the expression is false.
Example 1
<?PHP// w w w. j a v a2 s. com
$myValue = 23;
if ( $myValue >= 10 ) {
echo "greater than 10.";
} else {
echo "Less than 10!";
}
?>
The code above generates the following result.
Example 2
You can even combine the else statement with another if statement to make as many alternative choices as you like:
<?php/*from ww w.j a va2 s . c om*/
$Age = 23;
if ($Age < 10) {
print "You're under 10";
} elseif ($Age < 20) {
print "You're under 20";
} elseif ($Age < 30) {
print "You're under 30";
} elseif ($Age < 40) {
print "You're under 40";
} else {
print "You're over 40";
}
?>
The code above generates the following result.