Php -- switch statement

Source: Internet
Author: User
The switch statement is similar to a series of if statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement. The switch statement is similar to a series of if statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement.

Note: Unlike other languages, the function of the continue statement on the switch is similar to break. If there is a switch in the loop and you want to continue to the next loop in the outer loop, use continue 2.

Note:

Note that switch/case is a loose comparison.

The following two examples use two different methods to implement the same thing. one uses a series of if and elseif statements, and the other uses the switch statement:

Example #1 switch structure

 

Example #2 the switch structure can be a string

 

To avoid errors, it is very important to understand how the 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. For example:

 

If $ I is equal to 0, PHP will execute all the echo statements! If $ I is equal to 1, PHP will execute the following two echo statements. Only when $ I is equal to 2 will the expected result be returned -- only "I equals 2" will be displayed ". So don't forget that the break statement is very important (even if you deliberately want to avoid providing them in some cases ).

In the switch statement, the condition is evaluated only once and used to compare with each case statement. In the elseif statement, the condition is evaluated again. If the condition is much more complex than a simple one, or in a loop that occurs many times, the use of the switch statement may be faster.

The statement in a case can also be empty, which transfers the control to the statement in the next case.

 

The special case of a case is default. It matches any situations that do not match other cases. For example:

 

A case expression can be any simple expression, that is, an integer, a floating point, or a string. Arrays or objects cannot be used unless they are unbound from the simple type.

Switch supports process control of alternative syntax.

 

You can use a semicolon instead of the colon after the case statement. for example:

 

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.