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