In IF ELSE, PHP provides a way to do an either/or selection. What if you need more than two options? This is where ELSEIF comes in. ELSEIF is built on top of the IF ELSE construct to provide additional options. The basic syntax is as follows:
IF (conditional statement 1) {
[code if condition statement 1 is true]
}
ELSEIF (conditional statement 2) {
[code if condition statement 2 is true]
}
...
ELSE {
[code if none of the above is true]
}
ELSEIF can be repeated as many times as needed prior to the ELSE statement.
Let's use an example. Assuming we have the following piece of code:
$sample = 10; IF ($sample > 15) { print "Level 1"; } ELSEIF ($sample > 5) { print "Level 2"; } ELSE { print "Level 3"; } |
Level 2 |
Previous Page->PHP-if-else || Next Page->PHP-Switch