PHP switch statement Multiple values match the implementation of the same code block _php tutorial

Source: Internet
Author: User
Let's talk about the format of the switch () statement.

switch (expression) {

Case Match 1:
When matching 1 and expression matches the code executed successfully;
Break

Case Match 2:
When matching 2 and expression matches the code executed successfully;
Break
Default
If the case statement does not have the code executed with the expression success;
}

It is important to understand how switch is executed. The switch statement executes one line after the other (in fact, a statement). No code was executed at the beginning. Only when the value in one case statement matches the value of the switch expression does PHP begin executing the statement until the end of the switch's program segment or the first break statement is encountered. If you do not write a break at the end of the statement segment of the case, PHP continues to execute the statement segment in the next case.
Example:
Copy CodeThe code is as follows:
Switch ($i) {
Case 1:
The value of echo "$i is 1";
Break
Case 2:
The value of echo "$i is 2";
Break
Case 3:
The value of echo "$i is 3";
Break
Default
echo "$i value is not 1, 2, 3";
}
?>

The statement in one case can also be empty, so that only the control is transferred to the statement in the next scenario, knowing that the statement block of the next case is not empty, thus implementing multiple value matching consent blocks:
When the value of $i is 1 or 2 or 3 o'clock, the same statement is output:
Copy CodeThe code is as follows:
Switch ($i) {
Case 1:
Case 2:
Case 3:
The value of echo "$i is $i 1 or 2 or 3";
Break
}
?>

http://www.bkjia.com/PHPjc/736819.html www.bkjia.com true http://www.bkjia.com/PHPjc/736819.html techarticle first, the format of the switch () statement, switch (expression) {case Match 1: When matching 1 and expression matches the code executed successfully; break, case match 2: when match 2 and expression match successfully executed ...

  • 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.