We don’t need to introduce the difference between switch and ifelse. Here I will introduce to you the performance of switch and ifelse. When is it more suitable to use switch or ifelse?
There are two methods in PHP that are used to determine whether the value meets the conditions, and take different actions if it meets/is not met.
No matter what language you write a program in, you must consider the running efficiency of the code. After consulting some information, switch and ifelse each have superior efficiency in different 'environments'.
1. When the value being judged is a constant (fixed value), the operating efficiency of switch is higher than that of ifelse;
The code is as follows
|
Copy code
|
||||
switch($changliang){ case 1: echo 'The constant value is 1'; Break; // Break out of the loop Case 2: echo 'The constant value is 2'; break; case 3: echo 'The constant value is 3'; break; } |
2. When the judged value is a variable, the operating efficiency of ifelse is higher than that of switch. Ifelse implements the policy of judging from the first condition to the last else, so it is beneficial to learn to use switch;