Comparison of efficiency performance of switch and IfElse in PHP _php tutorial

Source: Internet
Author: User
About the difference between switch and ifelse we do not introduce, here I will introduce switch and IfElse performance bar, when in the end is more appropriate to use switch or ifelse it.

There are two methods in PHP that are used to determine whether a value satisfies a condition, and if it is satisfied/not satisfied to make a different action.

Regardless of the language of the program, will certainly take into account the operational efficiency of the Code. After reviewing some of the information, switch and ifelse in different ' environment ' efficiency each has the superiority.

1, when the value to be judged is a constant (fixed value), the operation efficiency of switch is higher than that of IfElse.

The code is as follows Copy Code

$changliang = 3; The value of variable judgment is constant
Switch ($changliang) {
Case 1:
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 judged value as a variable, ifelse operating efficiency is higher than switch,ifelse implementation of the policy, will start judging from the first condition, until the last else, so learn to use switch has the advantage;

The code is as follows Copy Code

$a = $_get[' a ']; Pass value after get pass value; The value to be 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 ';
}

http://www.bkjia.com/PHPjc/632684.html www.bkjia.com true http://www.bkjia.com/PHPjc/632684.html techarticle about the difference between switch and ifelse we do not introduce, here I will introduce switch and IfElse performance bar, when in the end is more appropriate to use switch or ifelse it. PHP has two of the parties ...

  • 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.