Структуры контроля


elseif / else if

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.

<?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