The PHP switch-case statement is an alternative to the if-elseif-else statement. PHP switch statement is used to execute one statement from multiple conditions.
switch(…){…} is the control structure block code
case value : case… are the blocks of code to be executed depending on the value of the condition
default : is the block of code to be executed when no value matches with the condition
<!DOCTYPE html>
<html>
<head>
<title>PHP Switch Statement</title>
</head>
<body>
<?php
$today = date("D");
switch($today){
case "Mon":
echo "Today is Monday.";
break;
case "Tue":
echo "Today is Tuesday.";
break;
case "Wed":
echo "Today is Wednesday.";
break;
case "Thu":
echo "Today is Thursday.";
break;
case "Fri":
echo "Today is Friday.";
break;
case "Sat":
echo "Today is Saturday.";
break;
case "Sun":
echo "Today is Sunday.";
break;
default:
echo "No information available for that day.";
break;
}
?>
</body>
</html>
The break
statement tells PHP to break out of the switch-case
statement block once it executes the code associated with the first true case.
Our website is made possible by displaying ads to our visitors.
Please help us continue to provide you with free. So please disabling your ad blocker.