The PHP switch loop, usually dealing with the conditional judgment of the compound, each child condition, is the case instruction part, usually the variable name. The ExprN after the case usually represents the value of the variable. The colon is followed by the part that meets the condition. Notice that you want to jump off the loop with a break.
Switch and if difference is
It's troublesome to use the IF loop. Of course, in the design, to the most likely to have the highest probability of the first, the least occurrence of the condition on the last side, you can increase the efficiency of program execution, Switch statements can avoid lengthy if. ElseIf.. else code block.
Switch (expr) {
Case EXPR1://Note here is a colon:
Statement1; Here is the semicolon;
Break Here is the semicolon;
Case EXPR2:
Statement2;
Break
:
:
Default
STATEMENTN;
Break
}
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
Look at an example
*/
$i = 5;
Switch ($i)
{
Case 1:
echo ' 1 ';
Break
Case 2:
echo ' 2 ';
Break
Case 3:
echo ' 5 ';
Break
Case 4:
echo $i;
Break
Default
echo ' CC ';
}
Switch method
Switch ($i)
{
Case 1:
Case 2:
Case 3:
$c = 555;
Break
Default
$c = 2;
}
See if Else
if ($i ==1 or $i ==3 or $i ==2)
{
$c = 555;
}
Else
{
$c = 2;
}
/*
From the above example, you can see if else is different from the switch case, as at the beginning of the article.
This article original in www.111cn.net reprint to indicate the origin