ในการเขียนโปรแกรม อาจจะมีเงือนไขหรือข้อกำหนดบางอย่างที่คุณต้องการให้โปรแกรมทำงานแตกต่างกันไป การตัดสินใจจึงเป็นเรื่องธรรมดาที่เกิดขึ้นทั้งในการเขียนโปรแกรมและในชีวิตประจำวัน
- if
- if...else
- if...elseif
- switch...case
ตัวอย่าง Arithmetic Operators
<?php
$number = 5;
if($number == 5){
echo "๕";
}
ตัวอย่าง if...else
<?php
$i = 1;
if($i <= 4) {
$text = "จำนวนน้อยกว่า 5";
}else{
$text = "จำนวนมากกว่า 4";
}
echo $text;
?>
ตัวอย่าง if...elseif
<?php
$test = "chaitawat";
if($test == "chaitawat"){
$text = "ชื่อ : ".$test;
}else if($test == "loongyod"){
$text = "นามสกุล : ".$test;
}else{
$text = "ข้อมูลผิดพลาด";
}
echo $text;
?>
ตัวอย่าง switch case
$abb = "th";
switch ($abb) {
case "de":
$country = "Germany";
break;
case "th":
$country = "Thailand";
break;
case "hu":
$country = "Hungary";
break;
case "tr":
$country = "Turkey";
break;
default:
$country = "Unknown country";
break;
}
echo "Your country is ".$country;
?>