The switch statement is used to perform different actions based on multiple different conditions.
If you want to selectively execute one of several blocks of code , use a switch statement.
<?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 label2 , the code here will be executed; } ?>
How it works: First, a simple expression n (usually a variable) is evaluated once. Compares the value of the 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 is executed, use break to prevent the code from jumping to the next case to continue execution. The default statement is executed when there is no match (that is, no case is true).
<?php $favcolor = " red " ; switch ( $favcolor ) { case " red " : echo " Your favorite color is red! " ; break ; case " blue " : echo " Your favorite color is blue! " ; break ; case " green " : echo " Your favorite color is green! " ; break ; default : echo " Your favorite color is not red, blue, or green! " ; } ?>