The difference of efficiency between switch and IfElse in PHP and the application analysis _php skills

Source: Internet
Author: User

This article analyzes the efficiency difference and application of switch and IfElse in PHP. Share to everyone for your reference. The specific analysis is as follows:

Both methods in PHP are used to determine whether a value satisfies a condition, and if it satisfies/does not satisfy a different behavioral 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;

$changliang =3;   Variable judgment value is constant
switch ($changliang) {
  Case 1:
    echo ' constant value is 1 ';
    break;   Jump out
  of circulation 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;

$a = $_get[' a ']; The value is received after the get pass value; The value of the judged value
if ($a =1) {
  echo ' variable A is 1 ';
} ElseIf ($a =2) {
  echo ' variable A has a value of 2 ';
} ElseIf ($a =3) {
  echo ' variable A has a value of 3 ';
}
  the value of else{echo ' variable A is unknown ';
}

I hope this article will help you with your PHP program design.

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.