The PHP switch syntax is used to perform actions based on different conditions.
-------------------------------------------------- ------------------------------
PHP for switch statements
Using the switch statement to select one of the many components of the code is executed.
Grammar
Switch (n)
{
Case Label1:
Code to is executed if n=label1;
Break
Case LABEL2:
Code to is executed if n=label2;
Break
Default
The code to was executed if N is different from both Label1 and Label2;
}
This is how it works: First, we have a single expression, n (often a variable), that counts only once. The value of the expression is then compared with the values of each case in the structure. If there is a match, the code associated with the case is executed. Use the rest time to avoid automatic execution by entering the next code. The default statement is used if no match is found.
For example
<body>
<?php
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";
}
?>
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 IS executed
If expression is different
From both Label1 and Label2;
}
</body>
Working principle:
Evaluate an expression (usually a variable) once
Compare the value of an expression with the value of a case in a structure
If there is a match, the code associated with the case is executed
After the code executes, the break statement blocks the code from jumping into the next case to continue execution
If no case is true, the default statement is used
<?php
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";
}
?>
</body>