Home > Backend Development > PHP Tutorial > Comparison of efficiency and performance between switch and ifelse in PHP_PHP Tutorial

Comparison of efficiency and performance between switch and ifelse in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:49:51
Original
965 people have browsed it

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
 代码如下 复制代码

$changliang=3;             // 变判断的值为常量
switch($changliang){
    case 1:
        echo '常量值为1';
        break;             // 跳出循环
    case 2:
        echo '常量值为2';
        break;
    case 3:
        echo '常量值为3';
        break;
}

Copy code

 代码如下 复制代码

$a = $_GET['a'];            // 通过get传值后接值; 被判断的值
if($a=1){
    echo '变量a的值为1';
}elseif($a=2){
    echo '变量a的值为2';
}elseif($a=3){
    echo '变量a的值为3';
}else{
    echo '变量a的值为不知道';
}

$changliang=3; // Change the judged value to a constant
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;

The code is as follows Copy code
$a = $_GET['a']; // Pass the value through get followed by the value; the value to be judged
if($a=1){
echo 'The value of variable a is 1';
}elseif($a=2){
echo 'The value of variable a is 2';
}elseif($a=3){
echo 'The value of variable a is 3';
}else{
echo 'The value of variable a is unknown';
} http://www.bkjia.com/PHPjc/632684.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632684.htmlTechArticleWe 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 appropriate to use switch or ifelse? There are two methods in PHP...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template