PHP Lazy Boolean Evaluation

后端 未结 3 935
别那么骄傲
别那么骄傲 2020-12-10 00:52

I have a conditional statement thus:

if($boolean && expensiveOperation()){ ...}

Does PHP have lazy boolean evaluation, i.e. will it

相关标签:
3条回答
  • 2020-12-10 01:19

    PHP does have short circuit evaluation. Your example would be the proper use of it:

    http://en.wikipedia.org/wiki/Short-circuit_evaluation#Support_in_common_programming_languages

    0 讨论(0)
  • 2020-12-10 01:25

    Yes, PHP does short-circuit evaluation.

    0 讨论(0)
  • 2020-12-10 01:30

    Yes it does. It's called short-circuit evaluation. See the comments on the documentation page...

    As for the order, it performs the checks based on Operator Precedence and then left to right. So:

    A || B || C
    

    Will evaluate A first, and then B only if A is false, and C only if both A and B are false...

    But

    A AND B || C
    

    Will always evaluate B || C, since || has a higher precedence than AND (not true for &&).

    0 讨论(0)
提交回复
热议问题