We have explained a multi-choice elseif statement. Although this statement can be used for multiple judgments, many if statements need to be written during use, which makes the readability of our program inconvenient, to avoid too many if statements and improve the readability of our program, you can use multiple switch judgment statements. in this chapter, we will introduce the "switch statement ". We have explained a multi-choice elseif statement. Although this statement can be used for multiple judgments, many if statements need to be written during use, which makes the readability of our program inconvenient, to avoid too many if statements and improve the readability of our program, you can use multiple switch judgment statements. in this chapter, we will introduce them to you.
"Switch statement".
Switch statement syntax:
switch (expr){ case expr1: statement; break; case expr2: statement; break; …… default: statement;}
Syntax explanation:
The switch statement matches the expr1. in sequence with the expr1... if the comparison is not equal, continue to find the next case. if the comparison is equal, execute the corresponding PHP statement until the switch statement ends or a break is encountered. There is a default at the end of the switch statement. If no matching condition is found in all cases, the statement following the default statement is output, which is similar to the else statement.
Switch statement instance
In this example, multiple elseif statements are used first, and the switch statement described in this chapter is used again. Compare the two methods. You can see that writing is easier. The code is as follows:
Elseif statement writing
Switch statement syntax:
The running result is as follows:
The above two methods can be used to produce the same running Results. However, the elseif statements write a lot of if statements, and the code does not seem easy to read, switch statements are much more refined.
The above is a detailed description of the "switch" statement instance of the PHP control statement. For more information, see other related articles in the first PHP community!