php PHP中&和&&的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2376348/
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 06:14:28  来源:igfitidea点击:

Difference between & and && in PHP

php

提问by Moon

I am confused with &and &&. I have two PHP books. One says that they are same, but the another says they are different. I thought they are same as well.

我对&和感到困惑&&。我有两本 PHP 书籍。一个说它们相同,但另一个说它们不同。我还以为他们是一样的。

Aren't they same?

他们不是一样的吗?

回答by cletus

&is bitwise AND. See Bitwise Operators. Assuming you do 14 & 7:

&是按位与。请参阅按位运算符。假设你这样做14 & 7

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

&&is logical AND. See Logical Operators. Consider this truth table:

&&是逻辑与。请参阅逻辑运算符。考虑这个真值表:

 $a     $b     $a && $b
false  false    false
false  true     false
true   false    false
true   true     true

回答by Matthew Flaschen

The other answers are correct, but incomplete. A key feature of logical AND is that it short-circuits, meaning the second operand is only evaluated if necessary. The PHP manual gives the following example to illustrate:

其他答案是正确的,但不完整。逻辑 AND 的一个关键特性是它会短路,这意味着仅在必要时才评估第二个操作数。PHP手册给出了以下例子来说明:

$a = (false && foo());

foowill never be called, since the result is known after evaluating false. On the other hand with

foo永远不会被调用,因为在评估 false 后结果是已知的。另一方面与

$a = (false & foo());

foowill be called (also, the result is 0 rather than false).

foo将被调用(同样,结果是 0 而不是 false)。

回答by Christos Lytras

Matthew's answerabout how Logical And&&operator is the biggest difference; logical comparison will stop when it will find something that breaks the chain. In addition, one more big difference it the result type/value.

马修关于逻辑与&&运算符的最大区别的回答;逻辑比较会在找到打破链条的东西时停止。此外,还有一个更大的区别是结果类型/值

tl;dr

tl;博士

By using the Logical And&&, it will always return a Boolean type/value, trueor false.

通过使用Logical And&&,它将始终返回一个布尔类型/值truefalse

false & 1 // int(0)
false && 1 // bool(false)

It is important to use Boolean type/valueswhen returning a function with a logical result, because someone can use the Identical comparison operator===to compare the results (which is high likely to happen) and it will fail if you use something like this:

在返回具有逻辑结果的函数时使用布尔类型/值很重要,因为有人可以使用相同的比较运算符===来比较结果(这种情况很可能发生),如果您使用这样的东西,它将失败:

(false & 1) === false // bool(false)
(true & true) === true // bool(false)

Never use Bitwise And&when you need to make a logical comparison and especially when returning values from functions with logical results. Instead use the Logical And&&:

当您需要进行逻辑比较时,尤其是从具有逻辑结果的函数返回值时,切勿使用Bitwise And&。而是使用逻辑与&&

(false && 1) === false // bool(true)
(true && true) === true // bool(true)

When comparing characters, Logical And&&will always result to true, even with NULcharacter, unless if it's converted to an integer:

比较字符时,逻辑与&&总是会导致true,即使是NUL字符,除非它被转换为整数:

'A' && 'B' // bool(true)
'A' && 0 // bool(false)
'A' && '
'A' & 'B' // string(1) "@"

01000001 // ASCII 'A'
&
01000010 // ASCII 'B'
=
01000000 // ASCII '@'
' // bool(true) 'A' && (int)'
1.0 & 1.0 // int(1)
2.0 & 1.0 // int(0)

1.0 && 1.0 // bool(true)
2.0 && 1.0 // bool(true)
' // bool(false)

If you use the Bitwise And&with characters, it will result the character corresponding to the Bitwise Andoperation between those two characters:

如果对字符使用Bitwise And&,则会在这两个字符之间产生对应于Bitwise And运算的字符:

int a = 0;
int b = 1;
int c = 2;

if (a & b)
    c = 3;

if (a && b)
    c = 4;

Beware the usage of the Bitwise And&when using with types other than Integersand Characters(which are special kind of integers). For example, if you use it with real numbers float/double, then it can result to 0even if both operands are NOT0:

当与整数字符(它们是特殊类型的整数)以外的类型一起使用时,请注意Bitwise And 的&使用。例如,如果你用它与实数浮点/双精度,那么它可能会导致到即使两个操作数是不是00

:0229  mov <a>, 0
:0230  mov <b>, 1
:0237  mov <c>, 2
// if (a & b) begins
:023E  mov eax, <a>
:0241  and eax, <b>        // a bitwise and b, result stored to eax
:0244  test eax, eax       // test eax and set ZeroFlag if equals to 0
:0246  je 024F             // >---  Jump if ZeroFlag is set
:0248  mov <c>, 3          //    |  or set c = 3
// if (a && b) begins            |
:024F  cmp <a>, 0          // <---  compare a to 0 and sets ZeroFlag if difference is 0
:0253  je 0262             // >---  Jump if ZeroFlag is set (a == 0)
:0255  cmp <b>, 0          //    |  compare b to 0 and sets ZeroFlag if differemce is 0
:0259  je 0262             //    |  >--- Jump if ZeroFlag is set (b == 0)
:025B  mov <c>, 4          //    |     | or set c = 4
:0262  <program continues> // <---  <---

In addition, if we go at assembly instructions level, we can see that difference and how the compiler manages to handle so the Logical And&&uses cmp <var>, 0to compare and does not continue executing if one operand fails; Bitwise Anduses and <var1>, <var2>to make a bitwise result and then test if it's of 0value. I know this question is tagged for phpand phpbehavior may be different than c, but I'll use a small cprogram to demonstrate how compiler behaves when using Logicaland Bitwise And.

此外,如果我们进入汇编指令级别,我们可以看到差异以及编译器如何处理,因此逻辑与&&用于cmp <var>, 0比较并且如果一个操作数失败则不会继续执行;Bitwise And用于and <var1>, <var2>生成按位结果,然后测试它是否0有价值。我知道这个问题被标记为php并且php行为可能与c不同,但我将使用一个小的c程序来演示编译器在使用LogicalBitwise And时的行为。

Let's assume we have a program in cthat uses both Bitwiseand Logical And:

让我们假设我们有一个使用按位逻辑与的c程序:

 

AND operation: 

& -> will do the bitwise AND operation , it just doing operation based on
      the bit values. 
&&   -> It will do logical AND operation. It is just the check the values is 
       true or false. Based on the boolean value , it will evaluation the 
       expression 

The compiler will generate the following assembly opcodes (W32Dasm result for x86; I have changed the memory addresses with <variable>names for simplicity and to be more understandable):

编译器将生成以下程序集操作码(x86 的 W32Dasm 结果<variable>为了简单起见,我已使用名称更改了内存地址,以便更易于理解):

##代码##

The compiler not only uses different instructions to compare between the Logicaland Bitwaise And, but at the line :0253in if (a && b)logical comparison, we see that if a == 0then it jumps and does not check for the rest operands.

编译器不仅使用不同的指令在LogicalBitwaise And之间进行比较,而且:0253if (a && b)逻辑比较的行中,我们看到 if a == 0then 它跳转并且不检查其余操作数。

So, I disagree to animuson's comment:

所以,我不同意animuson的评论

They are both the same thing, they're just used for two different things to accomplish the same task. – animuson Mar 4 '10 at 1:42

它们都是同一件事,它们只是用于完成相同任务的两种不同事物。– animuson 2010 年 3 月 4 日 1:42

They are not the same thing and both are/(should be)used for specific tasks depending on the programs' logic/flow.

它们不是一回事,两者都/ (应该)用于特定任务,具体取决于程序的逻辑/流程。

回答by Pavunkumar

##代码##

回答by umar

As the others are saying, a single &is bit-wise. It basically converts the left-hand value into its bits representation, and the right hand side into bits representation as well, then performs logical AND between them and outputs the result.

正如其他人所说,单曲&是按位计算的。它基本上将左侧的值转换为其位表示,将右侧的值也转换为位表示,然后在它们之间执行逻辑与并输出结果。

Double &&is either true or false, (in some languages 0 or 1) if both left and right side are true (or non-zero).

&&如果左侧和右侧都为真(或非零),则Double为真或假(在某些语言中为 0 或 1)。

I'd also add that this is not just in PHP. It is like that in many many other languages as well, like C, Java, Ruby, etc.

我还要补充一点,这不仅仅是在 PHP 中。在许多其他语言中也是如此,例如 C、Java、Ruby等。

回答by blobbo

&&is &performed on operands reduced to either 1or 0.

&&&对操作数减少到要么执行10

(In other words, &&is a bitwise operator under the caveat that it changes its operands. That is, logical operations are a subset of bitwise operations.)

(换句话说,&&是一个按位运算符,但需要注意的是它会更改其操作数。也就是说,逻辑运算是按位运算的子集。)