PHP 5.4.31 Released

elseif/else if

(PHP 4, PHP 5)

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 部分,其中第一个表达式值为 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;

?>

add a note add a note

User Contributed Notes 6 notes

up
15
Vladimir Kornea
7 years ago
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.
up
-13
Anonymous
7 years ago
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.
up
-12
peter dot mlich at volny dot cz
3 months ago
To  Rudi / 3 years ago

Try switch in switch($name) case 'word': break; . --- slow
Try if/else and  if/elseif in $name='word'. --- in my fast test, place 3
Try isset in isset($array[$name]). ---  place 1
Try in_array in in_array($name,$array). --- slow
Try array_key_exists in array_key_exists($name,$array).

Try return (end function) in if/elseif with  -- place 2
if('word'==$name) {
  $parsed[$name]=$text;
  return;
  } 
elseif('word'==$name) {
  $parsed[$name]=$text;
  return;


PHP 5.35, xml_parse function, i parsed 9.2MB xml file to sql, script in place 1 do it at 11.54s (do more than only condition)
up
-27
Anonymous
6 months ago
In addition to what Rudi  wrote,
I ran his test script  on Debian 6 with php 5.3.26 where his findings are true, elseif is faster than else if.
Then I ran his script on Windows 7 with php 5.4.7, where results are the opposite, else if is faster than elseif.
up
-36
Anonymous
7 months ago
The word 'elseif' is a not a combination of "if and else," but a combination of "else and if."
up
-58
Rudi
3 years ago
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
To Top