This article mainly introduces the differences in efficiency and applicability between switch and ifelse in php, and analyzes the differences in efficiency between switch and ifelse in the form of an instance in the case of variables and constants, very practical and required
This article mainly introduces the differences in efficiency and applicability between switch and ifelse in php, and analyzes the differences in efficiency between switch and ifelse in the form of an instance in the case of variables and constants, very practical and required
This article analyzes the efficiency differences between the switch and ifelse in php and their applicability. Share it with you for your reference. The specific analysis is as follows:
Both methods in PHP are used to determine whether the value meets the conditions. If the value meets/does not meet the conditions, different actions are performed.
No matter what language programs are written, the code running efficiency will be taken into account. After reading some materials, switch and ifelse have different efficiency advantages in different 'environment.
1. When the determined value is a constant (fixed value), the switch operation efficiency is higher than the ifelse operation efficiency;
$ Changliang = 3; // The variable judgment value is constant switch ($ changliang) {case 1: echo 'constant value is 1'; break; // jump out of loop case 2: echo 'constant value: 2'; break; case 3: echo 'constant value: 3'; break ;}
2. When the determined value is a variable, ifelse runs more efficiently than switch. ifelse implements the final judgment policy and starts to judge from the first condition until the last else, therefore, it is advantageous to learn to use a switch;
$ A = $ _ GET ['a']; // get the value after passing the value through GET; if ($ a = 1) {echo 'variable a value is 1';} elseif ($ a = 2) {echo 'variable a value is 2';} elseif ($ a = 3) {echo 'the value of variable a is 3';} else {echo 'the value of variable a is unknown ';}
I hope this article will help you with php programming.