elseif else if的区别

建议使用else if。

php中

(PHP 4, PHP 5)

elseif,和此名称暗示的一样,是 if 和 else 的组合。和 else 一样,它延伸了 if 语句,可以在原来的 if 表达式值为 FALSE 时执行不同语句。但是和else 不一样的是,它仅在 elseif 的条件表达式值为 TRUE 时执行语句。


必须要注意的是 elseif 与 else 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;

?>


你可能感兴趣的:(else,if)