PhpSwitch Statement
The switch statement is used to perform different actions based on several different conditions.
If you want to selectively execute one of several blocks of code , use the switch statement.
Grammar
<?php
Switch (n)
{
Case Label1:
If N=label1, the code here will be executed;
Break
Case LABEL2:
If N=label2, the code here will be executed;
Break
Default
If n is neither equal to Label1 nor equal to Label2, the code will be executed here;
}
?>
How it works: first evaluate a simple expression n(usually a variable). Compares the value of an expression to the value of each case in the structure. If there is a match, the code associated with the case is executed. After the code executes, use break to prevent the code from jumping into the next case to continue execution. The default statement is used when no match exists (that is, no case is true) when executed.
Instance
<?php
$favcolor = "Red";
Switch ($favcolor)
{
Case "Red":
echo "The color you like is red!";
Break
Case "Blue":
echo "The color you like IS blue!";
Break
Case "Green":
echo "The color you like IS green!";
Break
Default
echo "The color you like is not red, blue, or green!";
}
?>
The output is: The color you like is red!
PHP Switch Statement