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


add a note add a note User Contributed Notes
nospam at donireland dot com
26-Oct-2006 11:04
<?
if ($a==1){
 
/* nested if */
 
if ($b==1) {echo "2";}
}elseif (
$a==3){
 
/* other code */
}
?>

"Bug" solved.
jsimlo
15-Aug-2006 10:02
This example generates a parse error:
<?
if ($a==1):
 
/* nested if */
 
if ($b==1) echo "2";
elseif (
$a==3):
 
/* other code */
endif;
?>

The nested "if" binds to the outer "elseif" and the colon the generates an error: syntax error, unexpected ':'.

According to http://bugs.php.net/bug.php?id=838 this "bug" is not going to be fixed. Beware.

An "artful" sollution to this bug could look like this:

<?
if ($a==1):
 
/* nested if */
 
if ($b==1) echo "2";
 
/* dummy expression */
 
;
elseif (
$a==3):
 
/* other code */
endif;
?>
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.
mega0m3ga at yahoo dot com
10-Apr-2005 11:44
Dont make your code as jumbled up as this:

if (!$username || !$password) {
   echo("<form method='post' action='signin.php'>");
} elseif($action == "c" || $action == "d" || $action == "e" || $action == "f" || $action == "g" || $action == "h" || $action == "i") {
   echo("<form method='post' action='" . findbattle() . ".php'><input type='hidden' name='strt' value='y'>");
} elseif($action == "b") {
   die ("Sorry, dude. You're banned from this site!");
} else {
   echo("<form method='post' action='game.php'>");
}

It works best when you only compare related variables.