PHP Switch Case usage and example tutorial

Source: Internet
Author: User
Tags php switch switch case switch loop

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.