PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

流程控制的替代语法> <else
Last updated: Mon, 26 Nov 2007

view this page in

elseif

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 时执行。



流程控制的替代语法> <else
Last updated: Mon, 26 Nov 2007
 
add a note add a note User Contributed Notes
elseif
jouimet at uwaterloo dot BLOCK_IT dot ca
20-Apr-2008 04:36
Based on a test averaged over one hundred attempts (whereby one billion iterations occured during each attempt) on the alternative "else if" and "elseif" methods, I have observed that the "elseif" method completes its task more than 99% faster than the "else if" method.

This test was completed on a MacBook Pro with Mac OS 10.4.11 running an Apache server with PHP 5.2.3.

The conditional statements used within the iterations were the following (with $x = 10):

<?php
if ($x = 0) {
  
$res = 'f';
} else if (
$x = 1 || $x = 2 || $x = 3 || $x = 4 || $x = 5 || $x = 6 || $x = 7 || $x = 8 || $x = 9 || $x = 10) {
  
$res = 'v';
}
?>

<?php
if ($x = 0) {
  
$res = 'f';
} elseif (
$x = 1 || $x = 2 || $x = 3 || $x = 4 || $x = 5 || $x = 6 || $x = 7 || $x = 8 || $x = 9 || $x = 10) {
  
$res = 'v';
}
?>
01-Feb-2007 06: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
28-Dec-2006 01: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.
phpprogrammer at artspad dot net
11-Aug-2006 01:58
The comment critizing matheo's code while making some perhaps interesting statements about code efficiency missed the point completely.

echo 0; is different then echo false;
echo 0 will print a 0
echo false will print nothing.

So, the ternary operator technique was simply assuring there would be a displayable value.

If anything matheo showed prowess as a programmer and that he was knowledgable about nuances in php programming by knowing how to use the ternary operator to provide values true and false that are displayable.

Perhaps a clearer example of this would have been:
$is_a_bigger = ($a > $b) ? "true" : "false" ;

It is true that the result of a logic expression is a value like any other, it is a value though of a specific type which if using === can be checked for boolean logic which may or may not be what a programmer wants.

The general caveat if we are going to pontificate to programmers is to know exacly what you are getting from any operation and know the consequences of doing so. Which I think matheo knew quite well, and appropriately accounted for.

流程控制的替代语法> <else
Last updated: Mon, 26 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites