Comparison of efficiency performance between switch and IfElse in PHP

Source: Internet
Author: User
Tags constant

In PHP, two methods are used to determine whether a value satisfies a condition, and if it satisfies/does not satisfy a different action.

No matter what language you write the program, you will certainly take into account the efficiency of the operation of the code. After consulting some information, switch and IfElse have the advantage of efficiency under different ' environment '.

1, when the value is determined to be constant (fixed value), switch operation efficiency is higher than ifelse operation efficiency;

  code is as follows copy code

$changliang =3;             //Variable judgment value is constant
switch ($changliang) {
    Case 1:
    & nbsp;   echo ' constant value is 1 ';
        break;             //Jump out of the loop
    Case 2:
        echo ' constant value is 2 ';
        break;
    Case 3:
        echo ' constant value is 3 ';
        break;
}

2, when the value is judged as variable, ifelse operating efficiency is higher than switch,ifelse implementation of the end of the policy, will be judged from the first condition, until the last else, so learn to use switch is beneficial;

The code is as follows Copy Code

$a = $_get[' a ']; The value is received after the get pass value; The value of being judged
if ($a =1) {
The value of ECHO ' variable A is 1 ';
}elseif ($a =2) {
The value of ECHO ' variable A is 2 ';
}elseif ($a =3) {
The value of ECHO ' variable A is 3 ';
}else{
The value of ECHO ' variable A is not known ';
}

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.