首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

elseif/else if

(PHP 4, PHP 5, PHP 7)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

代码语言:javascript
复制
<?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";
}
?>

There may be several elseif_s within the same _if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.

Note: 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.

代码语言:javascript
复制
<?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;

?>

← else

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com