經常需要在滿足某個條件時執行一條語句,而在不滿足該條件時執行其它語句,這正是
else 的功能。else 延伸了
if 語句,可以在 if
語句中的表達式的值為 FALSE 時執行語句。例如以下代碼在
$a 大於 $b 時顯示
a is bigger than b,反之則顯示
a is NOT bigger than b:
<?php if ($a > $b) { echo "a is bigger than b"; } else { echo "a is NOT bigger than b"; } ?>
else 語句僅在 if 以及
elseif(若果有的話)語句中的表達式的值為
FALSE 時執行(參見 elseif)。