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 '; } |