elseif/else if
elseif/else if
, as its name suggests, is a combination of and . Like , it extends an statement to execute a different statement in case the original expression evaluates to false. However, unlike , it will execute that alternative expression only if the conditional expression evaluates to true. For example, the following code would display , or :
`elseififelseelseififelse``elseif````php
There may be several s within the same
statement. The first
expression (if any) that evaluates to
true would be executed. In PHP, it's possible to write
(in two words) and the behavior would be identical
to the one of (in a single word). The syntactic meaning
is slightly different (the same behavior as C) but the bottom line
is that both would result in exactly the same behavior.
`elseif``if``elseif``else if``elseif`
The statement is only executed if the
preceding expression and any preceding
expressions evaluated to
false, and the current
expression evaluated to
true.
`elseif``if``elseif``elseif`
> **Opmerking:** > Note that and
> will only be considered exactly the same when using curly brackets
> as in the above example. When using a colon to define
> / conditions, the use
> of in a single word becomes necessary. PHP
> will fail with a parse error if
> is split into two words.
> `elseif``else if``if``elseif``elseif``else if`
```php
<?php
/* Incorrect Method: */
if ($a > $b):
echo $a." is greater than ".$b;
else if ($a == $b): // Will not compile.
echo "The above line causes a parse error.";
endif;
<?php
/* Correct Method: */
if ($a > $b):
echo $a." is greater than ".$b;
elseif ($a == $b): // Note the combination of the words.
echo $a." equals ".$b;
else:
echo $a." is neither greater than or equal to ".$b;
endif;
?>