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

前端 未结 6 1075
礼貌的吻别
礼貌的吻别 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 04:41

    The question is not about having too many if else statements but 1) the order of them and 2) how efficient your conditions are

    1) If else status should be in descending order of probability of the conditions. In my particular wp_posts database table, 80% of the records have post_status of "draft", "pending", "trash" etc. but not "publish". About 40% the records have "post" as post_type. So, it would make no sense to have

    if ($post_type=="post"&&$post_status=="publish") {
        doA();
    } elseif ($post_type!="post"&&post_status=="publish") {
        doB():
    } elseif ($post_type=="post"&&post_status!="publish") {
        doC();
    } else {
        doD();
    }
    

    but it should goes in reverse order

    Regarding 2), the database schema of WordPress suggests that in_category() will be slow. If it's some query that you write yourself and of course, it depends on how efficient your query is

提交回复
热议问题