PHP鎺у埗缁撴瀯涔媏lseif/else if
鍐呭鎽樿
elseif锛屽拰姝ゅ悕绉版殫绀虹殑涓鏍凤紝鏄 if 鍜 else 鐨勭粍鍚堛傚拰 else 涓鏍凤紝瀹冨欢浼镐簡 if 璇彞锛屽彲浠ュ湪鍘熸潵鐨 if 琛ㄨ揪寮忓间负 FALSE 鏃舵墽琛屼笉鍚岃鍙ャ備絾鏄拰 else 涓嶄竴鏍风殑鏄紝瀹冧粎鍦 elseif 鐨勬潯浠惰〃杈惧紡鍊间负 TRUE 鏃舵墽琛岃鍙
鏂囩珷姝f枃
elseif/else if
elseif锛屽拰姝ゅ悕绉版殫绀虹殑涓鏍凤紝鏄 if 鍜 else 鐨勭粍鍚堛傚拰 else 涓鏍凤紝瀹冨欢浼镐簡 if 璇彞锛屽彲浠ュ湪鍘熸潵鐨 if 琛ㄨ揪寮忓间负 FALSE 鏃舵墽琛屼笉鍚岃鍙ャ備絾鏄拰 else 涓嶄竴鏍风殑鏄紝瀹冧粎鍦 elseif 鐨勬潯浠惰〃杈惧紡鍊间负 TRUE 鏃舵墽琛岃鍙ャ備緥濡備互涓嬩唬鐮佸皢鏍规嵁鏉′欢鍒嗗埆鏄剧ず a is bigger than b锛宎 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 璇彞銆傜涓涓〃杈惧紡鍊间负 TRUE 鐨 elseif 璇彞锛堝鏋滄湁鐨勮瘽锛夊皢浼氭墽琛屻傚湪 PHP 涓紝涔熷彲浠ュ啓鎴愨渆lse if鈥濓紙涓や釜鍗曡瘝锛夛紝瀹冨拰鈥渆lseif鈥濓紙涓涓崟璇嶏級鐨勮涓哄畬鍏ㄤ竴鏍枫傚彞娉曞垎鏋愮殑鍚箟鏈夊皯璁稿尯鍒紙濡傛灉浣犵啛鎮 C 璇█鐨勮瘽锛岃繖鏄悓鏍风殑琛屼负锛夛紝浣嗘槸搴曠嚎鏄袱鑰呬細浜х敓瀹屽叏涓鏍风殑琛屼负銆
elseif 鐨勮鍙ヤ粎鍦ㄤ箣鍓嶇殑 if 鎴 elseif 鐨勮〃杈惧紡鍊间负 FALSE锛岃屽綋鍓嶇殑 elseif 琛ㄨ揪寮忓间负 TRUE 鏃舵墽琛屻
Note: 蹇呴』瑕佹敞鎰忕殑鏄 elseif 涓 else if 鍙湁鍦ㄧ被浼间笂渚嬩腑浣跨敤澶ф嫭鍙风殑鎯呭喌涓嬫墠璁や负鏄畬鍏ㄧ浉鍚 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.
<?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;
?>
elseif锛屽拰姝ゅ悕绉版殫绀虹殑涓鏍凤紝鏄 if 鍜 else 鐨勭粍鍚堛傚拰 else 涓鏍凤紝瀹冨欢浼镐簡 if 璇彞锛屽彲浠ュ湪鍘熸潵鐨 if 琛ㄨ揪寮忓间负 FALSE 鏃舵墽琛屼笉鍚岃鍙ャ備絾鏄拰 else 涓嶄竴鏍风殑鏄紝瀹冧粎鍦 elseif 鐨勬潯浠惰〃杈惧紡鍊间负 TRUE 鏃舵墽琛岃鍙ャ備緥濡備互涓嬩唬鐮佸皢鏍规嵁鏉′欢鍒嗗埆鏄剧ず a is bigger than b锛宎 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 璇彞銆傜涓涓〃杈惧紡鍊间负 TRUE 鐨 elseif 璇彞锛堝鏋滄湁鐨勮瘽锛夊皢浼氭墽琛屻傚湪 PHP 涓紝涔熷彲浠ュ啓鎴愨渆lse if鈥濓紙涓や釜鍗曡瘝锛夛紝瀹冨拰鈥渆lseif鈥濓紙涓涓崟璇嶏級鐨勮涓哄畬鍏ㄤ竴鏍枫傚彞娉曞垎鏋愮殑鍚箟鏈夊皯璁稿尯鍒紙濡傛灉浣犵啛鎮 C 璇█鐨勮瘽锛岃繖鏄悓鏍风殑琛屼负锛夛紝浣嗘槸搴曠嚎鏄袱鑰呬細浜х敓瀹屽叏涓鏍风殑琛屼负銆
elseif 鐨勮鍙ヤ粎鍦ㄤ箣鍓嶇殑 if 鎴 elseif 鐨勮〃杈惧紡鍊间负 FALSE锛岃屽綋鍓嶇殑 elseif 琛ㄨ揪寮忓间负 TRUE 鏃舵墽琛屻
Note: 蹇呴』瑕佹敞鎰忕殑鏄 elseif 涓 else if 鍙湁鍦ㄧ被浼间笂渚嬩腑浣跨敤澶ф嫭鍙风殑鎯呭喌涓嬫墠璁や负鏄畬鍏ㄧ浉鍚 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.
<?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;
?>
浠ヤ笂灏辨槸PHP鎺у埗缁撴瀯涔媏lseif/else if鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒