控制结构
在线手册:中文 英文
PHP手册

elseif/else if

elseif,和此名称暗示的一样,是 ifelse 的组合。和 else 一样,它延伸了 if 语句,可以在原来的 if 表达式值为 FALSE 时执行不同语句。但是和 else 不一样的是,它仅在 elseif 的条件表达式值为 TRUE 时执行语句。例如以下代码将根据条件分别显示 a is bigger than ba 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 语句。第一个表达式值为 TRUEelseif 语句(如果有的话)将会执行。在 PHP 中,也可以写成“else if”(两个单词),它和“elseif”(一个单词)的行为完全一样。句法分析的含义有少许区别(如果你熟悉 C 语言的话,这是同样的行为),但是底线是两者会产生完全一样的行为。

elseif 的语句仅在之前的 ifelseif 的表达式值为 FALSE,而当前的 elseif 表达式值为 TRUE 时执行。

Note: 必须要注意的是 elseifelse if 只有在类似上例中使用大括号的情况下才认为是完全相同 Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

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


/* 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;

?>


控制结构
在线手册:中文 英文
PHP手册
PHP手册 - N: elseif/else if

用户评论:

lmeurs (08-Feb-2012 05:36)

As Vladimir Kornea stated, this does not work:

<?php
if($a):
    echo
$a;
    if(
$b) {
      echo
$b;
    }
else:
    echo
$c;
endif;
?>

But when you put a semicolon after the inner if, PHP knows that the following else belongs to the outer if. Like this:

<?php
if($a):
    echo
$a;
    if(
$b) {
      echo
$b;
    };
// <- closing semicolon
else:
    echo
$c;
endif;
?>

Rudi (14-Sep-2010 09:15)

Note that } elseif() { is somewhat faster than } else if() {

===================================
Test (100,000,000 runs):
<?php
$start
= microtime(true);
for(
$i = 0; $i < 100000000; $i++) {
    if(
2 === 0) {} else if(2 === 1) {} else {}
}
$end = microtime(true);
echo
"1: ".($end - $start)."\n";
unset(
$start, $end);

$start = microtime(true);
for(
$i = 0; $i < 100000000; $i++) {
    if(
2 === 0) {} elseif(2 === 1) {} else {}
}
$end = microtime(true);
echo
"2: ".($end - $start);
?>

===================================
Result (depending on hardware configuration):
1: 20.026723146439
2: 20.20437502861

(31-Jan-2007 10:54)

There is no good way to interpret the dangling else.  One must pick a way and apply rules based on that. 

Since there is no endif before an else, there is no easy way for PHP to know what you mean.

Vladimir Kornea (27-Dec-2006 05:59)

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.