PHP Switch Case
Switch case is a selectional statement which is used to perform different task depending on different conditions.
Syntax :
switch (user_input) {
case name1:
will be executed when user_input=name1;
break;
case name2:
will be executed when user_input=name2;
break;
case name3:
will be executed when user_input=name3;
break;
...
default:
will be executed if user_input does not match any of the case names;
}
Explaination : The user gives an input which we have stored in a variable named user_input. The value of user_input will be matched with each case name to check if it has a match. Whichever case name matches, that block of code will be executed.If the user_input doesn't match with any of the case names then the default case will be executed.
Here is an example of Switch case
Example:
<?php
$user_input = "morning";
switch ($user_input) {
case "morning":
echo "time for breakfast";
break;
case "afternoon":
echo "time for lunch";
break;
case "night":
echo "time for dinner";
break;
default:
echo "Input does not match any case";
}
?>
The Output for the above code :
time for breakfast