问题描述
我有一个代码库,开发人员决定使用 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'&&'作为运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!