Home  >  Article  >  Backend Development  >  PHP Switch Statement Study Notes_PHP Tutorial

PHP Switch Statement Study Notes_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:12:34970browse

The Switch statement in PHP is used to perform different actions based on several different conditions.

Switch Statement

Use the Switch statement if you want to selectively execute one of several blocks of code.

Use Switch statements to avoid lengthy if..elseif..else blocks.

Syntax

Copy code The code is as follows:

switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default :
code to be executed
if expression is different
from both label1 and label2;
}


Instance

Working principle:

Perform a calculation on the expression (usually a variable)
Compare the value of the expression with the value of the case in the structure
If there is a match, execute the same as the case Associated code
After the code is executed, the break statement prevents the code from jumping to the next case to continue execution
If no case is true, use the default statement

Copy the code The code is as follows:

switch ($x)
{
case 1:
echo "Number 1" ;
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default :
echo "No number between 1 and 3";
}
?>



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313613.htmlTechArticleThe Switch statement in PHP is used to perform different actions based on multiple different conditions. Switch Statement Use the Switch statement if you want to selectively execute one of several blocks of code. Use...
Statement:
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