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.