㈠ php如何運用if else分別執行三條代碼並輸出各自的結果
PHP中既有else if又有elseif,詳細用法如下:
elseif,和此名稱暗示的一樣,是 if 和 else 的組合。和 else 一樣,它延伸了 if 語句,可以在原來的 if 表達式值為 FALSE 時執行不同語句。但是和 else 不一樣的是,它僅在 elseif 的條件表達式值為 TRUE 時執行語句。
例如以下代碼將根據條件分別顯示
a is bigger than b,a equal to b 或者a is smaller than b:
if($a>$b){
echo"aisbiggerthanb";
}elseif($a==$b){
echo"aisequaltob";
}else{
echo"aissmallerthanb";
}
/*不正確的使用方法:*/
if($a>$b):
echo$a."isgreaterthan".$b;
elseif($a==$b)://將無法編譯
echo"Theabovelinecausesaparseerror.";
endif;
/*正確的使用方法:*/
if($a>$b):
echo$a."isgreaterthan".$b;
elseif($a==$b)://注意使用了一個單詞的elseif
echo$a."equals".$b;
else:
echo$a."isneithergreaterthanorequalto".$b;
endif;
㈡ php關於if 裡面else if 在嵌套一個if,怎麼做
第一:
php的標准if語句語法是:
if(){
}elseif(){
}else{
}
建議寫php代碼別偷懶, 寫齊了還是更好!
二、if語句一般用於相對簡單的判斷, 如果判斷過多, 建議用switch
如:
$a=10;
switch($a){
case5:echo'小';break;
case10:echo'對了'break;
default:echo'是的';
}
或者;
$a=10;
switch(true){
caseis_numeric($a):echo'是數字';break;
caseis_object($a):echo'是對象';break;
default:echo'不知道是什麼';
}