The Switch statement in PHP is used to execute different actions based on multiple different conditions. Switch statement if you want to selectively execute one of several code blocks, use the Switch statement. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else. Syntax switch (expression) {caselabel1: codetobeexe
The Switch statement in PHP is used to execute different actions based on multiple different conditions. Switch statement if you want to selectively execute one of several code blocks, use the Switch statement. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else. Syntax switch (expression) {case label1: code to be exe
The Switch statement in PHP is used to execute different actions based on multiple different conditions.
Switch statement
If you want to selectively execute one of several code blocks, use the Switch statement.
Use the Switch statement to avoid lengthy code blocks such as if... elseif... else.
Syntax
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 expression value with the case value in the Structure
If a match exists, execute the code associated with the case
After the code is executed, the break statement prevents the code from jumping into the next case for further execution.
If no case is true, use the default statement.
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 ";
}
?>