Example of an application where multiple values of the phpswitch statement match the same code block

Source: Internet
Author: User
Tags case statement
The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning. The following is an example. For more information, see

The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning. The following is an example. For more information, see

First, let's talk about the format of the switch () statement.

Switch (expression ){

Case match 1:

Code that is successfully executed when match 1 and expression match;

Break;

Case matching 2:

Code that is successfully executed when matching 2 and expression match;

Break;

Default:

If the case statement does not have the code successfully executed with the expression;

}

It is very important to understand how a switch is executed. The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning. PHP starts to execute the statement only when the value in a case statement matches the value of the switch expression until the switch program segment ends or the first break statement is encountered. If break is not written at the end of the case statement segment, PHP will continue to execute the statement segment in the next case.

Example:

<? Phpswitch ($ I) {case 1: echo "$ I value is 1"; break; case 2: echo "$ I value is 2"; break; case 3: echo "$ I value is 3"; break; default: echo "$ I value is not 1, 2, 3" ;}?>

The statement in a case can also be empty, which transfers the control to the statement in the next case, knowing that the statement block in the next case is not empty ,, in this way, multiple values match and agree to the code block:

When $ I is 1, 2, or 3, the same statement is output:

<? Phpswitch ($ I) {case 1: case 2: case 3: echo "$ I is 1, 2, or 3"; break;}?>

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.