PHP Switch
The switch statement is used to perform different actions based on multiple different conditions.
PHP Switch Statement
Use the switch statement if you want to selectively execute one of several blocks of code .
Syntax
switch (n) { case label1: 如果 n=label1,此处代码将执行; break; case label2: 如果 n=label2,此处代码将执行; break; default: 如果 n 既不等于 label1 也不等于 label2,此处代码将执行; }works: First, a simple expression n (usually a variable) is calculated once. Compares 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 to the next case to continue execution. default statement is used to execute when there is no match (that is, no case is true).
Instance
<?php $favcolor="red"; switch ($favcolor) { case "red": echo "你喜欢的颜色是红色!"; break; case "blue": echo "你喜欢的颜色是蓝色!"; break; case "green": echo "你喜欢的颜色是绿色!"; break; default: echo "你喜欢的颜色不是 红, 蓝, 或绿色!"; } ?>
Run Instance»
Click the "Run Instance" button to view the online instance
Recommended practical tutorials: "PHP Switch Statement"