else
Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to ENTITY. For example, the following code would display a is greater than b if $a is greater than $b, and a is NOT greater than b otherwise:
<?php
$a = 3;
$b = 5;
if ($a > $b) {
echo "a is greater than b\n";
} else {
echo "a is NOT greater than b\n";
}
ENTITY
a is NOT greater than b
The else statement is only executed if the if expression evaluated to ENTITY, and if there were any elseif expressions - only if they evaluated to ENTITY as well (see elseif).
[!NOTE] In case of nested
if-elsestatements, anelseis always associated with the nearestif.<?php $a = false; $b = true; if ($a) if ($b) echo "b"; else echo "c";ENTITY
Despite the indentation (which does not matter for PHP), the
elseis associated with theif ($b), so the example does not produce any output. While relying on this behavior is valid, it is recommended to avoid it by using curly braces to resolve potential ambiguities.