PHP switch Statement Basic Syntax example detailed

Source: Internet
Author: User
Tags case statement php switch
The switch statement is used to perform different actions based on different conditions. Here are some knowledge points for switch usage, and friends who are interested in switch usage learn through this article. Switch is a toggle statement, switch statement and have the sameAn expression a series of IF statements are similar. In many cases it is necessary to compare the same variable (or expression) with many different values and execute different code depending on which value it equals. This is the purpose of the switch statement.

Note: Note that unlike other languages, the continue statement acts on a switch as if it were a break. If there is a switch in the loop and want to continue to the next reincarnation in the outer loop, use continue 2.

The following two examples use two different methods to accomplish the same thing, one with a series of if statements, and the other with a switch statement:

Switch structure

<?PHPIF ($i = = 0) {echo "I equals 0";} ElseIf ($i = = 1) {echo "I equals 1";} ElseIf ($i = = 2) {echo "I equals 2";} Switch ($i) {case 0:echo "I equals 0", break, Case 1:echo "I equals 1", break, Case 2:echo "I equals 2"; >

The switch structure can use a string

<?phpswitch ($i) {case "Apple": echo "I am Apple"; break, Case "bar": echo "I am bar"; case "cake": echo "I am C Ake "; break;}? >

To avoid errors, it is important to understand how the 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 (such as a return statement) 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. For example:

<?phpswitch ($i) {case 0:echo "I equals 0", Case 1:echo "I equals 1", Case 2:echo "I equals 2";}? >

Special Note: here if $i equals 3,php will not execute any ECHO statement! However, if $i equals 0,php will execute all the echo statements! If $i equals 1,php, the following two echo statements are executed. Only if the $i equals 2 o'clock will the result be "expected"-only "I equals 2" is displayed. So, don't forget that break statements are important (even if you deliberately want to avoid providing them in some cases).

In a switch statement, the condition is a value only once and is used to compare with each case statement. The condition is evaluated again in the ElseIf statement. If the condition is much more complex than a simple comparison or in a multiple loop, then the switch statement may be faster.

The statement in one case can also be empty, so that the control is simply transferred to the statement in the next scenario.

<?phpswitch ($i) {case 0:case 1:case 2:echo "I was less than 3 and not negative", break, Case 3:echo "I was 3";}? >

The exception to a case is default. It matches anything that doesn't match any other case. For example:

<?phpswitch ($i) {case 0:echo "I equals 0", break, Case 1:echo "I equals 1", break, Case 2:echo "I equals 2"; Default:echo "I was not equal to 0, 1 or 2";}? >

The case expression can be any expression that evaluates to a simple type, that is, an integer or floating-point number, and a string. Arrays or objects cannot be used unless they are dereferenced as simple types.

Related Article

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.