Elseif Construct in PHP
The elseif construct allows
you to specify conditions in the else block.
Let's look at an example:
<?php
$num = 1;
if ($num === 1) {
echo 'variant 1';
} elseif ($num === 2) {
echo 'variant 2';
} elseif ($num === 3) {
echo 'variant 3';
}
?>
The advantage of using elseif
instead of multiple if statements is the ability
to catch the situation when the value of the variable
$num does not match any of the conditions:
<?php
$num = 1;
if ($num === 1) {
echo 'variant 1';
} elseif ($num === 2) {
echo 'variant 2';
} elseif ($num === 3) {
echo 'variant 3';
} else {
echo 'variant not supported';
}
?>
The variable $day contains some number
from the interval 1 to 31. Determine
into which decade of the month this number falls
(the first, second, or third).
Modify the previous task so that,
if the variable $day contains not a number
from 1 to 31, an error message
is displayed.