'AND'vs'&&'作为运算符 [英] 'AND' vs '&&' as operator

查看:115
本文介绍了'AND'vs'&&'作为运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码库,开发人员决定使用 AND ,而不是& ;& ||



我知道运算符优先(&& 在之前),但使用给定的框架( PrestaShop 是明确的)它显然不是一个原因。



您使用哪个版本?是&& 更易读吗? c>和

如果使用 AND

code> OR
,你最终会被这样的事情所困扰:

  $ this = true; 
$ that = false;

$ truthiness = $ this和$ that;

想猜猜 $ truthiness 等于什么?



如果你说 false ... bzzzt,对不起,错了!


$上面的b $ b

$ truthiness 具有值 true 。为什么? = 更高的优先级。添加括号以显示隐式顺序使得这更清楚:

 ($ truthiness = $ this)和$ that 

如果您使用&&& c $ c>和在第一个代码示例中,它将按预期工作,并且 false



如下面的注释中所讨论的,这也可以获得正确的值,因为括号的优先级高于 =

  $ truthiness =($ this和$ that)


I have a codebase where developers decided to use AND and OR instead of && and ||.

I know that there is a difference in operators' precedence (&& goes before and), but with the given framework (PrestaShop to be precise) it is clearly not a reason.

Which version are you using? Is and more readable than &&? Or is there no difference?

解决方案

If you use AND and OR, you'll eventually get tripped up by something like this:

$this = true;
$that = false;

$truthiness = $this and $that;

Want to guess what $truthiness equals?

If you said false... bzzzt, sorry, wrong!

$truthiness above has the value true. Why? = has a higher precedence than and. The addition of parentheses to show the implicit order makes this clearer:

($truthiness = $this) and $that

If you used && instead of and in the first code example, it would work as expected and be false.

As discussed in the comments below, this also works to get the correct value, as parentheses have higher precedence than =:

$truthiness = ($this and $that)

这篇关于'AND'vs'&&'作为运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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