-2

类似于如何缩短加号和减号:

$x = $x + 5;

变成

$x += 5;

你能用位运算符做类似的事情吗?例如,在应用 XOR 时,以下内容是否有效?

$x = $x ^ 1;

变成

$x ^= 1;

测试了这个简单的脚本后,它似乎可以工作,但是使用它是否正确,或者我在这里偏离了轨道?

4

2 回答 2

1

是的,这是正确的。

来自http://www.php.net/manual/en/language.operators.assignment.php(第一条评论)

See the Arithmetic Operators page (http://www.php.net/manual/en/language.operators.arithmetic.php)
Assignment    Same as:
$a += $b      $a = $a + $b    Addition
$a -= $b      $a = $a - $b    Subtraction
$a *= $b      $a = $a * $b    Multiplication
$a /= $b      $a = $a / $b    Division
$a %= $b      $a = $a % $b    Modulus

See the String Operators page(http://www.php.net/manual/en/language.operators.string.php)
$a .= $b      $a = $a . $b       Concatenate

See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php)
$a &= $b      $a = $a & $b     Bitwise And
$a |= $b      $a = $a | $b     Bitwise Or
$a ^= $b      $a = $a ^ $b     Bitwise Xor
$a <<= $b     $a = $a << $b    Left shift
$a >>= $b     $a = $a >> $b    Right shift
于 2013-03-25T11:14:27.563 回答
0

是的,它在赋值运算符页面的评论中提到

Assignment    Same as:
$a += $b     $a = $a + $b    Addition
$a -= $b     $a = $a - $b     Subtraction
$a *= $b     $a = $a * $b     Multiplication
$a /= $b     $a = $a / $b    Division
$a %= $b     $a = $a % $b    Modulus

See the String Operators page(http://www.php.net/manual/en/language.operators.string.php)
$a .= $b     $a = $a . $b       Concatenate

See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php)
$a &= $b     $a = $a & $b     Bitwise And
$a |= $b     $a = $a | $b      Bitwise Or
$a ^= $b     $a = $a ^ $b       Bitwise Xor
$a <<= $b     $a = $a << $b     Left shift
$a >>= $b     $a = $a >> $b      Right shift
于 2013-03-25T11:13:48.887 回答