elseif is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.
The elseif statement cannot be used without if statement. Multiple elseif statements can be used in a single if statement.
Structure of elseif
Синтаксис | Описание |
---|---|
if(expr1) stmt1; elseif(expr2) stmt2; elseif(expr3) stmt3; else stmt4; |
1)stmt1 will be executed if expr1 is TRUE 2)stmt2 will be executed if expr2 is TRUE 3)stmt3 will be executed if expr3 is TRUE 4) stmt4 will be executed if none of expr1, expr2 or expr3 is TRUE |
<?php
$var1 = 1;
$var2 = 2;
$var3 = 3;
if($var1 == 0) // expression is FALSE
echo "var1 = 0";
elseif($var2 == 0) // expression is FALSE
echo "var2 = 0";
elseif($var3 == 0) // expression is FALSE
echo "var3 = 0";
elseif($var3 == 3) // expression is TRUE
echo "var3 = 3"; // statement will be executed
else
echo "No Result";
?>
[result]
var3 = 3