PHP switch statement Multiple values matching the same code block implementation _php Tips

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

switch (expression) {

Case Match 1:
When matching 1 and the expression match the successful execution of the Code;
Break

Case Match 2:
When matching 2 and the expression match the successful execution of the Code;
Break
Default
If the case statement does not have the code executed with the expression successfully;
}

It is very important to understand how switch is performed. The switch statement executes one line at a line (actually a statement-taking statement). No code is executed at the beginning. PHP begins executing a statement only if the value in one case statement matches the value of the switch expression until the switch's program segment ends or the first break statement is encountered. If you do not write a break at the end of the statement section of the case, PHP will continue to execute the statement section in the next box.
Example:
Copy Code code as follows:

<?php
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
The value of echo "$i is not 1, 2, 3";
}
?>

The statement in a case can also be empty, which simply transfers control to the statement in the next box, knowing that the statement block for the next case is not empty, so that multiple values match the consent code block is implemented:
Output the same statement when the value of $i is 1 or 2 or 3 o'clock:
Copy Code code as follows:

<?php
Switch ($i) {
Case 1:
Case 2:
Case 3:
echo "$i value of $i is 1 or 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.