Table of Contents
Who is faster, PHP switch or ifelse? " >Who is faster, PHP switch or ifelse?
Home Backend Development PHP Problem Which one is better, PHP switch or ifelse?

Which one is better, PHP switch or ifelse?

Jun 10, 2021 pm 05:38 PM
php

This article will introduce you to switch and ifelse in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Which one is better, PHP switch or ifelse?

Who is faster, PHP switch or ifelse?

For multiple if condition judgments, we use switch instead of ifelse, which will make the code clearer. So what is their efficiency comparison? From the PHP manual, I found that someone had already compared it, and I also experimented with his code:

$s = time();
for ($i = 0; $i < 1000000000; ++$i) {
    $x = $i % 10;
    if ($x == 1) {
        $y = $x * 1;
    } elseif ($x == 2) {
        $y = $x * 2;
    } elseif ($x == 3) {
        $y = $x * 3;
    } elseif ($x == 4) {
        $y = $x * 4;
    } elseif ($x == 5) {
        $y = $x * 5;
    } elseif ($x == 6) {
        $y = $x * 6;
    } elseif ($x == 7) {
        $y = $x * 7;
    } elseif ($x == 8) {
        $y = $x * 8;
    } elseif ($x == 9) {
        $y = $x * 9;
    } else {
        $y = $x * 10;
    }
}
print("if: " . (time() - $s) . "sec\n");

$s = time();
for ($i = 0; $i < 1000000000; ++$i) {
    $x = $i % 10;
    switch ($x) {
        case 1:
            $y = $x * 1;
            break;
        case 2:
            $y = $x * 2;
            break;
        case 3:
            $y = $x * 3;
            break;
        case 4:
            $y = $x * 4;
            break;
        case 5:
            $y = $x * 5;
            break;
        case 6:
            $y = $x * 6;
            break;
        case 7:
            $y = $x * 7;
            break;
        case 8:
            $y = $x * 8;
            break;
        case 9:
            $y = $x * 9;
            break;
        default:
            $y = $x * 10;
    }
}
print("switch: " . (time() - $s) . "sec\n");
Copy after login

After looping 1000000000 times and adding operations to each judgment condition, we found the result It is switch that is more efficient and runs faster. The result on my computer is:

// if: 301sec
// switch: 255sec
Copy after login

Although switch is more efficient, there are also things that need to be paid attention to. First of all, the judgment value can only be Number, floating point number or string. Secondly, each judgment is an ordinary == judgment, that is to say, the following judgment results are not necessarily the same as yours:

$string = "2string";

switch ($string) {
    case 1:
        echo "this is 1";
        break;
    case 2:
        echo "this is 2";
        break;
    case &#39;2string&#39;:
        echo "this is a string";
        break;
}

// this is 2
Copy after login

Yes, it is still a type cast during == comparison. The problem is that when comparing string and int values, they are forced to the int type. The result of the forced conversion of "2string" is exactly 2. Therefore, when using switch, you should ensure that the comparison value is consistent with the type of each case, otherwise unpredictable errors may occur.

Reference code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/PHP%E7%9A%84switch%E5%92%8Cifelse%E8%B0%81%E6%9B%B4%E5%BF%AB%EF%BC%9F.php
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of Which one is better, PHP switch or ifelse?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles