PHP if...else 語句
PHP - if...else 語句
當條件為真時,if...else
語句會執行一些程式碼,否則會執行另一段程式碼。
語法
if (condition) {
// code to be executed if condition is true;
} else {
// code to be executed if condition is false;
}
示例
噹噹前時間小於 20 時,輸出 "Have a good day!",否則輸出 "Have a good night!"
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
自己動手試一試 »
PHP - if...elseif...else 語句
對於兩個以上的條件,if...elseif...else
語句會執行不同的程式碼。
語法
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
// code to be executed if first condition is false and this condition is true;
} else {
// code to be executed if all conditions are false;
}
示例
噹噹前時間小於 10 時,輸出 "Have a good morning!",噹噹前時間小於 20 時,輸出 "Have a good day!"。否則將輸出 "Have a good night!"
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
自己動手試一試 »