PHP Switch Statement
A switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with several different values, and execute a different piece of code depending on which value it equals to. This is what the switch statement is for.
#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; }
Important points
#Example <?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 neither red, blue, nor green!"; } ?>