The Switch statement in PHP is used to perform different actions based on a number of different conditions.
Switch Statement
If you want to selectively execute one of several blocks of code, use the Switch statement.
Use the Switch statement to avoid lengthy if. ElseIf. else code block.
Grammar
Copy CodeThe code is as follows:
switch (expression)
{
Case Label1:
Code to is executed if expression = Label1;
Break
Case LABEL2:
Code to is executed if expression = Label2;
Break
Default
Code to be executed
If expression is different
From both Label1 and Label2;
}
Example
Working principle:
Perform a single calculation on an expression (usually a variable)
Compare the value of an expression with the value of a case in the structure
If there is a match, the code associated with the case is executed
After the code executes, the break statement prevents the code from jumping into the next case to continue execution
If no case is true, use the default statement
Copy CodeThe 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";
}
?>
http://www.bkjia.com/PHPjc/313613.html www.bkjia.com true http://www.bkjia.com/PHPjc/313613.html techarticle the Switch statement in PHP is used to perform different actions based on a number of different conditions. Switch statement If you want to selectively execute one of several blocks of code, use the switch statement. Use ...