The switch statement is executed one by one row (actually a statement). The following describes the format of the switch () statement when multiple values of the phpswitch statement match the same code block.
Switch (expression ){
Case Match 1:
Code that is successfully executed when match 1 and expression match;
Break;
Case matching 2:
Code that is successfully executed when matching 2 and expression match;
Break;
Default:
If the case statement does not have the code successfully executed with the expression;
}
It is very important to understand how a switch is executed. The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning. PHP starts to execute the statement only when the value in a case statement matches the value of the switch expression until the switch program segment ends or the first break statement is encountered. If break is not written at the end of the case statement segment, PHP will continue to execute the statement segment in the next case.
Example:
The code is as follows:
Switch ($ I ){
Case 1:
Echo "$ I value is 1 ";
Break;
Case 2:
Echo "$ I value is 2 ";
Break;
Case 3:
Echo "$ I value is 3 ";
Break;
Default:
Echo "$ I value is not 1, 2, 3 ";
}
?>
The statement in a case can also be empty. in this way, the control is transferred to the statement in the next case, knowing that the statement block in the next case is not empty, in this way, multiple values match and agree to the code block:
When $ I is 1, 2, or 3, the same statement is output:
The code is as follows:
Switch ($ I ){
Case 1:
Case 2:
Case 3:
Echo "$ I is 1, 2, or 3 ";
Break;
}
?>