This keyword uses both keywords if and else.
In this structure, in case the first if expression evaluates to FALSE.
It will execute alternative elseif expression.
else if is same as elseif.
Syntax of elseif
1 2 3 4 5 6 7 8 9 10 11 12 |
if(expression) { // statements } elseif { //statements } else { //statements } |
Flowchart of else if
There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write ‘else if’ (in two words) and the behavior would be identical to the one of ‘elseif’ (in a single word). The syntactic meaning is slightly different (if you’re familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.
The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.
Example of elseif-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $a = 10; $b = 10; if($a < $b) { echo "a is bigger than b"; } elseif($a > $b) { echo "b is bigger than a"; } else { echo "a is equal to b"; } ?> /* output: a is equal to b */ |
There may be several elseif within the same if statement