Let’s talk about the format of the switch() statement first
switch(expression){
case match 1:
When match 1 and expression match the code that is successfully executed;
break ;
case match 2:
code that is successfully executed when match 2 and expression match;
break;
default:
code that is executed successfully if case statement does not match expression Code;
}
It is very important to understand how switch is executed. The switch statements are executed line by line (actually statement by statement). Initially no code is executed. PHP starts executing the statement only if the value in a case statement matches the value of the switch expression, and continues until the end of the switch block or until the first break statement is encountered. If you do not write break at the end of the statement segment of the case, PHP will continue to execute the statement segment in the next case.
Example:
Copy code The code is as follows:
switch($i){
case 1:
echo "The value of $i is 1";
break;
case 2:
echo "The value of $i is 2";
break;
case 3:
echo "The value of $i is 3";
break;
default:
echo "The value of $i is not 1, 2, 3";
}
?>
The statement in one case can also be empty. This just transfers control to the statement in the next case until the statement block of the next case is not empty. This way This implements multiple value matching agreement code blocks:
Output the same statement when the value of $i is 1 or 2 or 3:
Copy code The code is as follows:
switch($i){
case 1:
case 2:
case 3:
echo "$i The value of $i is 1 or 2 or 3";
break;
}
?>
http://www.bkjia.com/PHPjc/736819.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736819.htmlTechArticleLet’s first talk about the format of the switch() statement switch(expression){ case matches 1: When matching 1 and expression The expression matches the successfully executed code; break; case match 2: When match 2 and the expression match the successfully executed code...