Does having a lot of if statements degrade rendering speed of php?

前端 未结 6 1074
礼貌的吻别
礼貌的吻别 2021-01-02 04:12

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?<

6条回答
  •  既然无缘
    2021-01-02 05:05

    I've just came across an very interesting piece of article from WPShout which uses a very human analogy to invite us to code smarter, and why deeply nested if { .. } statements might degrade not only performance but also the code maintainability. If the logic is performance aware, without unnecessary bloated iteration checks, chances are you'll end up with a pretty well organized, responsive-cleaner code.

    CODE EXAMPLE (extracted form the article)

    "Let’s make this concrete by looking at two simple code examples: first in bubble-style, then in gateway-style".

    Bad: Bubble-Style

    $is_first_thing_working = true;
    $is_second_thing_working = true;
    $is_third_thing_working = true;
    $is_fourth_thing_working = true;
    
    if( $is_first_thing_working === true ) {
        if( $is_second_thing_working === true ) {
            if( $is_third_thing_working === true ) {
                if( $is_fourth_thing_working === true ) {
                    return 'Working properly!';
                }
                else {
                    return 'Fourth thing broken.';
                }
            }
            else {
                return 'Third thing broken.';
            }
        }
        else {
            return 'Second thing broken.';
        }
    }
    else {
        return 'First thing broken.';
    }
    

    Good: Gateway-Style

    $is_first_thing_working = true;
    $is_second_thing_working = true;
    $is_third_thing_working = true;
    $is_fourth_thing_working = true;
    
    if( $is_first_thing_working !== true ) {
        return 'First thing broken.';
    }
    
    if( $is_second_thing_working !== true ) {
        return 'Second thing broken.';
    }
    
    if( $is_third_thing_working !== true ) {
        return 'Third thing broken.';
    }
    
    if( $is_fourth_thing_working !== true ) {
        return 'Fourth thing broken.';
    }
    
    return 'Working properly!';
    

    NOTES ON EXAMPLE

    The difference between the two code snippets above boils down to a key distinction:

    The bubble method asks if important conditions are true, and only runs code if they are true.

    The gateway method asks if important conditions are false, and immediately issues exit instructions for each condition if it’s false.

    The bubble method forces nesting, because you have to check “true, true, true, true” before you get to the code you want to run. Each “true” check is a level of nesting—a condition your code has to live inside.

    The gateway method is not nested: as you see, the code is never more than one layer of logic deep. This is because once a given gateway is passed, we can forget about it completely. In other words, since we didn’t exit after our $is_first_thing_working check, we automatically know that $is_first_thing_working is true for the rest of the code.

    "It’s like real life: if you’re sitting next to me in history class, I know you’re a human, a student at my high school, etc.—or else you would’ve never been in my class in the first place. No need to check."

提交回复
热议问题