PHP Switch Understanding

Source: Internet
Author: User

Prior to doing the project encountered a problem, the code is roughly as follows:

Switch ($a) {case $a >=1000 && $a <5000:echo "vip1";    Break        Case $a >=5000 && $a <=10000:echo "VIP2";    Break Default:echo "Vipx";

If $ A >0 is not a problem, when $ A =0, the problem will come, the program will output: VIP1;

Later, it should be the following:

The function of switch in PHP is: Choose to execute a block of code, which works as follows:

 1. A one-time calculation of expressions (usually variables);

2. Compare the value of the variable with the value of case in the switch structure;

3. If the value of the variable is equal to the value of the case, the code associated with it is executed;

4. When the code finishes executing, the break statement prevents the code from jumping into the next case to continue execution;

5. If there is no case condition to match, execute the code in default;

Instance code:

That is, when $ A =0, the first case first operation, $a >=1000 && $a <5000 Of course is false, and then 0 vs. false, which is true, so returned "VIP1";


This article is from the "7804265" blog, please be sure to keep this source http://7814265.blog.51cto.com/7804265/1820801

PHP Switch Understanding

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.