如果第一个参数在和条件下为true,则下一个运算符会运行吗? [英] If first argument falls true in a condition with and operators will the next run?

查看:109
本文介绍了如果第一个参数在和条件下为true,则下一个运算符会运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一些类似下面的代码:

Lets say I've got a pice of code that looks like this:

if( !isset($this->domainID) && !$this->getDomainID() ){
    return false;
}

如果第一个语句为true,第二条语句是否会运行?
因为从性能角度考虑,如果我已经从数据库中获取了ID,那将是很愚蠢的,并且在许多其他情况下,也是如此。如果不是,我就必须嵌套它们,对吗?

Will the 2nd statement run if the first one is true? Because performance wise it would be stupid to get the ID from the database if I've already got it and there's a lot of other situation where the same apply. If it doesn't I'd have to nest them, am I right?

我不知道在这些情况下编程语言的工作方式是否存在标准如果其他语言不同,则使用php。我尝试使用Google搜索,但是在这种情况下我真的不知道要搜索什么。如您所见,我在标题中很难描述它。

I don't know if there's an standard on how programming language work in these cases or if it's different in other languages then php. I tried googling it but I didn't really know what to search for in this case. As you can see I hade a quite hard time describing it in the title.

推荐答案

是的。如果第一个为真,则将评估第二个。相反,如果第一个为假,则第二个将不被评估。如前所述,这可能是进行微优化和逻辑渐进的好地方。

Yes. If the first is true, the second will be evaluated. Conversely, if the first is false, the second will not be evaluated. This can be a good place for micro optimizations and, as you noted, logical progression.

每个注释对于OR条件而言都是相反的。因此,如果第一个表达式为false,则将评估下一个表达式;如果第一个表达式为true,则将不对其进行评估。

Per the comments, the inverse is true for OR conditions. So if the first expression is false the next will be evaluated and if the first expression is true the next will not.

这篇关于如果第一个参数在和条件下为true,则下一个运算符会运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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