PHP惰性布尔评估 [英] PHP Lazy Boolean Evaluation

查看:76
本文介绍了PHP惰性布尔评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个条件语句:

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屋!

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