Home > Article > Backend Development > What is the usage of switch in php
In PHP, the switch statement is used to perform different actions based on multiple different conditions. The syntax "switch (expression) {case value 1: statement block 1; break;... case value n: statement block n;break;default: statement block n 1;}"; the value of the expression can only be an integer or a string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP Switch statement
The switch statement is used to perform different actions based on multiple different conditions.
The switch statement consists of an expression and multiple case labels. The case label is followed by a code block, and the case label serves as the identifier of this code block. The syntax format of the switch statement is as follows:
switch(表达式){ case 值 1: 语句块 1; break; case 值 2: 语句块 2; break; ... ... case 值 n: 语句块 n; break; default: 语句块 n+1; }
The switch statement will compare it with the value in the case in turn according to the value of the expression. If they are not equal, continue to search for the next case; if they are equal, the corresponding execution will be performed. statement until the switch statement ends or break is encountered.
Generally speaking, the switch statement ultimately has a default value default. If no matching condition is found in the previous case, the default statement will be executed, similar to the else statement.
The execution flow of the switch statement is shown in the figure below:
You should pay attention to the following points when using the switch statement:
The difference from the if statement is that the data type of the expression after the switch statement can only be integer or string, not bool. Usually this expression is a variable name. Although PHP is a weakly typed language, the variable in the expression after switch can be any type of data. However, in order to ensure the accuracy of matching execution, it is best to only use one of the integers or strings. type.
Different from the if statement, the curly braces after the switch statement are required.
The number of case statements is not specified and can be increased indefinitely. But there should be a space between the case label and the value after the case label, and there must be a colon after the value, which is part of the syntax.
After the switch matching is completed, the statements in the matched branch modules will be executed one by one, and execution will not stop until the switch structure ends or a break statement is encountered. Therefore, if there is no break statement written after a branch statement, the program will continue to execute the content of the next branch statement.
Similar to the else in the if statement, the default label in the switch statement is directly followed by a colon. It seems that there is no condition, but in fact it is conditional. The condition is the "expression" The value cannot be equal to the value after any previous case label, then the statement in the default branch will be executed. The default label is the same as the else clause in if. It is not required in the switch statement and can be omitted.
[Example] Use the date() function to get the English abbreviation of the current week, and print the day of the week based on the abbreviation. The code is as follows:
<?php header("content-type:text/html;charset=utf-8"); $week = date('D'); switch($week) { case 'Mon' : echo '星期一'; break; case 'Tue' : echo '星期二'; break; case 'Wed' : echo '星期三'; break; case 'Thu' : echo '星期四'; break; case 'Fri' : echo '星期五'; break; case 'Sat' : echo '星期六'; break; case 'Sun' : echo '星期日'; break; } ?>
The running results are as follows :
## Recommended study: "PHP Video Tutorial"
The above is the detailed content of What is the usage of switch in php. For more information, please follow other related articles on the PHP Chinese website!