Php--switch statements

Source: Internet
Author: User
Tags case statement
A switch statement is similar to a series of if statements that have the same expression. 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: Notice that unlike other languages, the continue statement acts on the switch as if it were a break. If there is a switch in the loop and want to continue to the next round loop in the outer loop, use continue 2.

Note:

Note that the switch/case is loosely compared.

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

Example #1 Switch Structure

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

Example #2 switch structure can be used in string

<?phpswitch ($i) {case "Apple":    echo "I am apple";    Break;case "Bar":    echo "I is bar";    Break;case "Cake":    echo "I is cake";    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 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";}? >

Here 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 am 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";        break;    Default:        echo "I am 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.

Switch supports process control for alternative syntax.

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

A semicolon is allowed instead of a colon after a case statement, for example:

<?phpswitch ($beer) {case    ' tuborg ';    Case ' Carlsberg ';    Case ' Heineken ';        Echo ' Good choice ';    break;    default;        Echo ' Please make a new selection ... ';    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.