• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • elseif/else if

    (PHP 4, PHP 5, PHP 7)

    elseif,和此名称暗示的一样,是ifelse的组合。和else一样,它延伸了if语句,可以在原来的if表达式值为FALSE时执行不同语句。但是和else不一样的是,它仅在elseif的条件表达式值为TRUE时执行语句。例如以下代码将根据条件分别显示a is bigger than b,a equal to b或者a is smaller than b:

    <?php
    if ($a > $b) {
        echo "a is bigger than b";
    } elseif ($a == $b) {
        echo "a is equal to b";
    } else {
        echo "a is smaller than b";
    }
    ?>
    

    在同一个if语句中可以有多个elseif部分,其中第一个表达式值为TRUE(如果有的话)的elseif部分将会执行。在 PHP 中,也可以写成“else if”(两个单词),它和“elseif”(一个单词)的行为完全一样。句法分析的含义有少许区别(如果你熟悉 C 语言的话,与之行为相同),但是底线是两者会产生完全一样的行为。

    elseif的语句仅在之前的if和所有之前elseif的表达式值为FALSE,并且当前的elseif表达式值为TRUE时执行。

    Note:必须要注意的是elseifelse if只有在类似上例中使用花括号的情况下才认为是完全相同。如果用冒号来定义if/elseif条件,那就不能用两个单词的else if,否则 PHP 会产生解析错误。

    <?php
    /* 不正确的使用方法: */
    if ($a > $b):
        echo $a." is greater than ".$b;
    else if ($a == $b): // 将无法编译
        echo "The above line causes a parse error.";
    endif;
    /* 正确的使用方法: */
    if ($a > $b):
        echo $a." is greater than ".$b;
    elseif ($a == $b): // 注意使用了一个单词的 elseif
        echo $a." equals ".$b;
    else:
        echo $a." is neither greater than or equal to ".$b;
    endif;
    ?>
    
    The parser doesn't handle mixing alternative if syntaxes as reasonably as possible.
    The following is illegal (as it should be):
    <?
    if($a):
      echo $a;
    else {
      echo $c;
    }
    ?>
    This is also illegal (as it should be):
    <?
    if($a) {
      echo $a;
    }
    else:
      echo $c;
    endif;
    ?>
    But since the two alternative if syntaxes are not interchangeable, it's reasonable to expect that the parser wouldn't try matching else statements using one style to if statement using the alternative style. In other words, one would expect that this would work:
    <?
    if($a):
      echo $a;
      if($b) {
       echo $b;
      }
    else:
      echo $c;
    endif;
    ?>
    Instead of concluding that the else statement was intended to match the if($b) statement (and erroring out), the parser could match the else statement to the if($a) statement, which shares its syntax.
    While it's understandable that the PHP developers don't consider this a bug, or don't consider it a bug worth their time, jsimlo was right to point out that mixing alternative if syntaxes might lead to unexpected results.
    The reason 'else if' (with a space) works with traditional syntax and not colon syntax is because of a technicality.
    <?php
     if($var == 'Whatever') {
     } else if($var == 'Something Else') {
     } 
    ?>
    In this instance, the 'else if' is a shorthand/inline else statement (no curly braces) with the if statement as a body. It is the same things as:
    <?php
     if($var == 'Whatever') {
     } else {
       if($var == 'Something Else') {
       }
     }
    ?>
    If you were to write this with colon syntax, it would be:
    <?php
     if($var == 'Whatever'):
     else:
       if($var == 'Something Else'):
       endif;
     endif;
    ?>
    

    上篇:else

    下篇:流程控制的替代语法