php “AND”与“&&”作为运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2803321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:42:38  来源:igfitidea点击:

'AND' vs '&&' as operator

phpoperatorsoperator-precedence

提问by ts.

I have a codebase where developers decided to use ANDand ORinstead of &&and ||.

我有一个代码库,开发人员决定使用ANDandOR代替&&and ||

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

我知道运营商的优先级存在差异(&&在之前and),但是对于给定的框架(准确地说是PrestaShop),这显然不是原因。

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

您使用的是哪个版本?是and不是更具有可读性&&?或者没有区别?

回答by Powerlord

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

如果你使用ANDand OR,你最终会被这样的事情绊倒:

$this_one = true;
$that = false;

$truthiness = $this_one and $that;

Want to guess what $truthinessequals?

想猜什么$truthiness等于?

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

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

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

$truthiness上面有值true。为什么? =具有比更高的优先级and。添加括号以显示隐式顺序使这一点更加清晰:

($truthiness = $this_one) and $that

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

如果您在第一个代码示例中使用了&&而不是and,它将按预期工作并且是false.

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

正如下面的评论中所讨论的,这也有助于获得正确的值,因为括号的优先级高于=

$truthiness = ($this_one and $that)

回答by adamJLev

Depending on how it's being used, it might be necessary and even handy. http://php.net/manual/en/language.operators.logical.php

根据它的使用方式,它可能是必要的,甚至是方便的。 http://php.net/manual/en/language.operators.logical.php

// "||" has a greater precedence than "or"

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;

But in most cases it seems like more of a developer taste thing, like every occurrence of this that I've seen in CodeIgniter framework like @Sarfraz has mentioned.

但在大多数情况下,这似乎更像是一种开发人员品味的东西,就像我在 CodeIgniter 框架中看到的每一种情况一样,@Sarfraz 都提到过。

回答by Captain Kenpachi

For safety, I always parenthesise my comparisons and space them out. That way, I don't have to rely on operator precedence:

为了安全起见,我总是将我的比较放在括号中并将它们隔开。这样,我就不必依赖运算符优先级:

if( 
    ((i==0) && (b==2)) 
    || 
    ((c==3) && !(f==5)) 
  )

回答by arviman

Precedence differs between &&and and(&& has higher precedence than and), something that causes confusion when combined with a ternary operator. For instance,

&&and之间的优先级不同(&& 的优先级高于 and),当与三元运算符结合使用时会导致混淆。例如,

$predA && $predB ? "foo" : "bar"

will return a stringwhereas

将返回一个字符串,

$predA and $predB ? "foo" : "bar"

will return a boolean.

将返回一个boolean

回答by Justinas

Since andhas lower precedence than =you can use it in condition assignment:

由于and优先级低于=您在条件分配中使用它的优先级:

if ($var = true && false) // Compare true with false and assign to $var
if ($var = true and false) // Assign true to $var and compare $var to false

回答by Mahmoud Zalt

Let me explain the difference between “and” - “&&” - "&".

让我解释一下“and” - “&&” - “&” 之间的区别。

"&&" and "and" both are logical AND operationsand they do the same thing, but the operator precedence is different.

"&&" 和 "and" 都是逻辑与操作,它们做同样的事情,但运算符优先级不同。

The precedence (priority) of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator.

运算符的优先级(优先级)指定它将两个表达式绑定在一起的“紧密程度”。例如,在表达式 1 + 5 * 3 中,答案是 16 而不是 18,因为乘法 ("*") 运算符的优先级高于加法 ("+") 运算符。

Mixing them together in single operation, could give you unexpected results in some casesI recommend always using &&, but that's your choice.

在单个操作中将它们混合在一起,在某些情况下可能会给您带来意想不到的结果,我建议始终使用 &&,但这是您的选择。



On the other hand "&" is a bitwise AND operation. It's used for the evaluation and manipulation of specific bits within the integer value.

另一方面,“&”是按位与操作。它用于评估和操作整数值中的特定位。

Example if you do (14 & 7) the result would be 6.

例如,如果您执行 (14 & 7),结果将为 6。

7   = 0111
14  = 1110
------------
    = 0110 == 6

回答by salathe

which version are you using?

你使用的是哪个版本?

If the coding standards for the particular codebase I am writing code for specifies which operator should be used, I'll definitelyuse that. If not, and the code dictateswhich should be used (not often, can be easily worked around) then I'll use that. Otherwise, probably&&.

如果我正在为其编写代码的特定代码库的编码标准指定应该使用哪个运算符,我肯定会使用它。如果没有,并且代码指示应该使用哪个(不经常使用,可以轻松解决),那么我将使用它。否则,可能&&

Is 'and' more readable than '&&'?

“and”比“&&”更易读吗?

Is it more readable to you. The answer is yes and nodepending on many factors including the code around the operator and indeed the person reading it!

对您来说是否更具可读性。答案是否定的取决于许多因素,包括操作员周围的代码以及阅读它的人!

|| there is ~ difference?

|| 有~区别?

Yes. See logical operatorsfor ||and bitwise operatorsfor ~.

是的。请参阅逻辑运算符||位运算符~

回答by nuqqsa

I guess it's a matter of taste, although (mistakenly) mixing them up might cause some undesired behaviors:

我想这是一个品味问题,尽管(错误地)将它们混合在一起可能会导致一些不良行为:

true && false || false; // returns false

true and false || false; // returns true

Hence, using && and || is safer for they have the highest precedence. In what regards to readability, I'd say these operators are universal enough.

因此,使用 && 和 || 更安全,因为它们具有最高优先级。在可读性方面,我会说这些运算符足够通用。

UPDATE: About the comments saying that both operations return false ... well, in fact the code above does not return anything, I'm sorry for the ambiguity. To clarify: the behavior in the second case depends on how the result of the operation is used. Observe how the precedence of operators comes into play here:

更新:关于评论说两个操作都返回 false ......好吧,实际上上面的代码没有返回任何东西,我很抱歉含糊不清。澄清:第二种情况下的行为取决于操作结果的使用方式。观察运算符的优先级如何在这里发挥作用:

var_dump(true and false || false); // bool(false)

$a = true and false || false; var_dump($a); // bool(true)

The reason why $a === trueis because the assignment operator has precedence over any logical operator, as already very well explained in other answers.

原因$a === true是因为赋值运算符优先于任何逻辑运算符,正如其他答案中已经很好地解释的那样。

回答by Ken

Another nice example using ifstatements without =assignment operations.

另一个使用if没有=赋值操作的语句的好例子。

if (true || true && false); // is the same as:
if (true || (true && false)); // TRUE

and

if (true || true AND false); // is the same as:
if ((true || true) && false); // FALSE

because ANDhas a lower precedence and thus ||a higher precedence.

因为AND具有较低的优先级,因此||具有较高的优先级。

These are different in the cases of true, false, falseand true, true, false. See https://ideone.com/lsqovsfor en elaborate example.

这是在的情况下,不同的true, false, falsetrue, true, false。有关详细示例,请参阅https://ideone.com/lsqovs

回答by Synchro

Here's a little counter example:

这是一个小反例:

$a = true;
$b = true;
$c = $a & $b;
var_dump(true === $c);

output:

输出:

bool(false)

I'd say this kind of typo is far more likely to cause insidious problems (in much the same way as =vs ==) and is far less likely to be noticed than adn/rotypos which will flag as syntax errors. I also find and/or is much easier to read. FWIW, most PHP frameworks that express a preference (most don't) specify and/or. I've also never run into a real, non-contrived case where it would have mattered.

我会说这种类型的拼写错误更有可能导致潜在的问题(=与 vs 的方式大致相同==),并且比adn/ro拼写错误更不容易被注意到,因为/拼写错误会标记为语法错误。我还发现和/或更容易阅读。FWIW,大多数表达偏好的 PHP 框架(大多数没有)指定和/或。我也从来没有遇到过一个真实的、非人为的案例,它会很重要。