PHP 5 switch Statement

switch statement is used to perform different actions based on a number of different conditions.

PHP Switch statement

If you want to selectively execute one of several code blocks , use a switch statement.


<?php switch (n) { case label1: in case n=label1,Here code will execute; break; case label2: in case n=label2,Here code will execute; break; default: in case n Neither equals label1 Not equal label2,Here code will execute; } ?>
How it works: First calculate a simple expression n (usually a variable). Compare 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 into the next case to continue execution. The default statement is used when no match exists (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 "The color you like is not Red, blue, or green!"; } ?>