Home > Backend Development > PHP Tutorial > Implementation of multiple values ​​​​matching the same code block in php switch statement_PHP tutorial

Implementation of multiple values ​​​​matching the same code block in php switch statement_PHP tutorial

WBOY
Release: 2016-07-13 10:37:05
Original
767 people have browsed it

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;
}
?>

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template