The Switch statement in PHP is used to perform different actions based on a number of different conditions.
You can avoid lengthy if by using the Switch statement frequently. ElseIf. else code block.
Instance
Working principle:
- Perform a single calculation on an expression (usually a variable)
- Compare the value of an expression with the value of a case in the structure
- If there is a match, the code associated with the case is executed
- After the code executes, the break statement prevents the code from jumping into the next case to continue execution
- If no case is true, use the default statement
<?php/* Time: 2014-09-14 * Gechong * Function: Switch Small instance * *///three variables to record three dice points $var1 = rand (1,6), $var 2 = rand (1,6); $var 3 = rand (1,6);//Village Home singing dice points $sum = $var 1+ $var $var 3;//used to determine whether the leopard $sum3 = $sum% 3;if ($sum 3 = = 0) { echo "Leopard, Dealer kill! "." <p> ";} else{ Switch ($sum) { case $sum <9: echo "on". $var 1. ",". " $var 2 ".", "." $var 3 "." <p> "." $sum "." Point "." <p> "." Buy Xiao Sheng ~_~ "." <p> "; break; Default: echo "on". $var 1. ",". " $var 2 ".", "." $var 3 "." <p> "." $sum "." Point "." <p> "." Buy a victory ^_^"." <p> "; break;}}? >
PHP Switch Statement