The switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing.
Syntax
switch(n){
case "person1":
// Code to be executed if n=person1
break;
case "person2":
// Code to be executed if n=person2
break;
...
default:
// Code to be executed if n is different from all
}
Consider the following case, which displays a different message for each day.
#example.php
<?php
$today = date("D");
switch($today){
case "Mon":
echo "Today is Monday. Clean your house.";
break;
case "Tue":
echo "Today is Tuesday. Buy some food.";
break;
case "Wed":
echo "Today is Wednesday. Visit a doctor.";
break;
case "Thu":
echo "Today is Thursday. Repair your car.";
break;
case "Fri":
echo "Today is Friday. Party tonight.";
break;
case "Sat":
echo "Today is Saturday. Its movie time.";
break;
case "Sun":
echo "Today is Sunday. Do some rest.";
break;
default:
echo "No information available for that day.";
break;
}
?>