The PHP switch Statement
The switch-case statement is used when performing one statement from numerous conditions.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Example
$fruits = "apple";
switch ($fruits) {
case "apple":
echo "This is a apple!";
break;
case "mango":
echo "This is a mango!";
break;
case "kiwi":
echo "This is a kiwi!";
break;
default:
echo "This is neither apple, mango, nor kiwi!";
}
In a switch statement, the block of code runs queue by queue and once a tome PHP finds a case statement that considers true, it’s not ending the execution, but also executes all the blocks of code till the end of the switch statement automatically.