The switch statement is used to perform different actions based on several different conditions.
PHP Switch Statement
If you want to selectively execute one of several blocks of code , use the switch statement.
Grammar
1 Switch(n)2 {3 CaseLabel1:4If n=Label1, here the code will be executed;5 Break;6 CaseLabel2:7If n=Label2, here the code will be executed;8 Break;9 default:Ten if n is neither equal to Label1 nor equal to Label2, the code will be executed here; One}
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 for nonexistent matches (that is, no
Case is true) is executed.
1<?PHP2 $favcolor= "Red";3 Switch($favcolor)4 {5 Case"Red":6 Echo"Your favorite color is red!";7 Break;8 Case"Blue":9 Echo"Your favorite color is blue!";Ten Break; One Case"Green": A Echo"Your favorite color is green!"; - Break; - default: the Echo"Your favorite color is neither red, blue, or green!"; - } -?>
11. PHP Tutorial _php Switch statement