简体   繁体   English

在PHP中分配变量值时,“&=”和“=&”之间有什么区别?

[英]What is the difference between “&=” and “=&” when assigning a variable value in PHP?

我知道=&通常意味着“通过引用分配”,但是如果我们反转这两个字符会发生什么,因为我在很多PHP脚本中看到了这一点?

$a &= $b$a = $a & $b缩写,它是按位和运算符。

It's the compound bitwise AND /assignment operator: 它是复合按位AND /赋值运算符:

$x = 0x01;
$y = 0x11;

$y &= $x; // bitwise AND $y and $x, assign result back to $y
var_dump($y == 0x01); // true

&= is a compound assignment operator , whereas =& is actually two separate operators ( = and & ), pushed together. &=是一个复合赋值运算符 ,而=&实际上是两个独立的运算符( =& ),被推到一起。 This is legal syntax because PHP doesn't demand whitespace between them. 这是合法的语法,因为PHP不要求它们之间的空格。

&= performs a bitwise AND operation between the left hand side and right hand side, then assigns the result to the left hand side variable. &=在左手侧和右手侧之间执行按位AND运算,然后将结果分配给左侧变量。

$x = 1;
$x &= 0; // $x === 0 now. A more verbose syntax would be "$x = $x & 0;"

On the other hand =& should really be expanded to = & as the operators are seperate. 另一方面=&应该真正扩展到= &因为运营商是分开的。 This is known as assignment by reference . 这被称为通过引用分配 The = is your standard assignment operator, and the & when prefixed before a variable name returns the reference to the variable. =是您的标准赋值运算符,并且在变量名称前面加前缀的&返回对变量的reference

$y = "foobar";
$x = &$y; // $x now holds a reference to $y.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM