是否有很多if语句会降低php的渲染速度? [英] Does having a lot of if statements degrade rendering speed of php?

查看:194
本文介绍了是否有很多if语句会降低php的渲染速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道PHP代码中复杂的if/else结构是否可能是一个错误的设计决策.是否有很多if语句会使PHP运行缓慢,网站加载速度变慢等?

I was wondering if complicated if/else structures in my PHP code could be a bad design decision. Does having a lot of if statements make PHP run slow, site load slower etc?

这是代码: (我不知道wordpress如何处理is_page等.)

This is the code: (I've no idea how wordpress handles is_page etc.)

<?php

if (is_page()) {
//
    if (is_page(122)) {
    //subscribe page
    echo "subscribe page";

    }

    elseif (is_page(1263)) {
    //photography course
    echo "photography course page";

    }

    elseif (is_page(array(210,184,128))) {
    //210 copyright policy, 184 privacy policy, 128 contact
    echo "this is either copyright policy, privacy or contact page!";
    //nothing happens here, we don't need social buttons on these pages.
    }

    elseif (is_page(array(379,71,7,45,124,8,105,175,9,125,110))) {
    //379 photo galleries, 71 car photos, 7 conceptual, 45 event photos, 124 fashion, 8 landscape, 105 misc, 175 journalism, 9 portrait, 125 street photography, 110 travel
    echo "gallery pages and albums";

    }

    else {
    //any other page
    echo "any other page";

    }
//
}

elseif (is_single()) {
//
    if (in_category(array(147,196,35))) {
    //147 car photography, 196 car wallpapers, 35 photo stories
    echo "photo posts";

    }
    else {
    //any other post
    echo "any other post";

    }
//
}

elseif (is_archive()) {
//
    //any category
    echo "this is archive template"

}
//
?>

推荐答案

不是.实际上,在大多数情况下,它实际上可以加快速度(因为允许跳过代码块).

Nope. In fact, it'll actually speed it up in most cases (because it's allowed to skip over blocks of code).

大量if语句会使速度变慢的唯一时间是您要检查的条件是否需要处理.例如:

The only time large numbers of if statements will slow it down is if the condition you're checking requires processing. An example would be something like:

while (true)
{
    if (count($some_array) == 0) { break; }
    /* some other code */
}

循环中的每次迭代都检查是否为count($some_array) == 0.这意味着每次通过PHP都必须手动count $some_array中的项目数,因为它可能已更改.这也适用于for循环中的停止条件.这是因为for循环始终可以重写为while循环:

Ever iteration through the loop checks if count($some_array) == 0. That means that every pass, PHP has to go and manually count the number of items in $some_array because it may have changed. This also applies to the stop condition in a for loop. This is because a for loop can always be rewritten as a while loop:

for ([INITIALIZER_ACTION]; [CONDITION]; [POST_ITERATION_ACTION]) { [CODE]; }

与...相同...

[INITIALIZER_ACTION];
while ([CONDITION]) { [CODE]; [POST_ITERATION_ACTION]; }

如果您正在考虑将一堆if语句合并为一个:不,您将不会获得任何好处. PHP会短路,这意味着如果到达知道结果的地方,它将跳过其余部分.

If you're considering merging a bunch of if statements into one: don't, you won't get any benefits. PHP does short circuiting which means that if it reaches a point where it knows what the outcome will be, it'll skip the rest.

例如,考虑$a = 5; if ($a > 0 || $b > 100 || $c > 200) {}.
一旦PHP看到满足$a > 0条件,整个语句就解析为true(由于使用OR值),并且不必费心检查$b > 100$c > 200.

For example, consider $a = 5; if ($a > 0 || $b > 100 || $c > 200) {}.
Once PHP sees that the $a > 0 condition is satisfied, the whole statement resolved to true (because of the usage of OR values) and doesn't bother to check $b > 100 or $c > 200.

因此回答您的问题:除非您有不合条件的条件数量,每个条件条件都需要复杂的计算或具有副作用,否则通常可以认为它们的数量无关紧要.
但是,正如其他人指出的那样,过多的if语句会降低代码的可读性.在许多情况下,如果您可以删除条件而不影响代码的行为,那么您就不需要它了.

So to answer your question: unless you have an ungodly number of conditionals that each require complicated calculations or have side effects, you can usually consider the quantity of them to be inconsequential.
However, as others have noted, having too many if statements can reduce code readability. In many cases, if you can remove a conditional without it affecting the behavior of the code, then you didn't need it to begin with.

这篇关于是否有很多if语句会降低php的渲染速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆