PHP惰性布尔评估 [英] PHP Lazy Boolean Evaluation
问题描述
因此,我有一个条件语句:
I have a conditional statement thus:
if($boolean && expensiveOperation()){ ...}
PHP是否具有懒惰的布尔值评估,即它将检查$boolean
,并且如果它为false,则不必费心执行昂贵的操作?如果是这样,我应该按什么顺序放置变量?
Does PHP have lazy boolean evaluation, i.e. will it check $boolean
and if it is false not bother performing the expensive operation? If so, what order should I put my variables?
推荐答案
是的.这称为短路评估.请参阅文档页面上的评论 ...
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
首先将评估A,然后仅在A为假时才评估B,仅在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...
但是
A AND B || C
将始终评估B || C
,因为||
的优先级高于AND
(对于&&
而言并非如此).
Will always evaluate B || C
, since ||
has a higher precedence than AND
(not true for &&
).
这篇关于PHP惰性布尔评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!