赋值运算符

基本的赋值运算符是"="。一开始可能会以为它是"等于",其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。

赋值运算表达式的值也就是所赋的值。也就是说,"$a = 3"的值是 3。这样就可以做一些小技巧:

<?php

$a 
= ($b 4) + 5// $a 现在成了 9,而 $b 成了 4。

?>

在基本赋值运算符之外,还有适合于所有二元算术,数组集合和字符串运算符的"组合运算符",这样可以在一个表达式中使用它的值并把表达式的结果赋给它,例如:

<?php

$a 
3;
$a += 5// sets $a to 8, as if we had said: $a = $a + 5;
$b "Hello ";
$b .= "There!"// sets $b to "Hello There!", just like $b = $b . "There!";

?>

注意赋值运算将原变量的值拷贝到新变量中(传值赋值),所以改变其中一个并不影响另一个。这也适合于在密集循环中拷贝一些值例如大数组。

在 PHP 中普通的传值赋值行为有个例外就是碰到对象 object 时,在 PHP 5 中是以引用赋值的,除非明确使用了 clone 关键字来拷贝。

引用赋值

PHP 支持引用赋值,使用"$var = &$othervar;"语法。引用赋值意味着两个变量指向了同一个数据,没有拷贝任何东西。

Example #1 引用赋值

<?php
$a 
3;
$b = &$a// $b 是 $a 的引用

print "$a\n"// 输出 3
print "$b\n"// 输出 3

$a 4// 修改 $a

print "$a\n"// 输出 4
print "$b\n"// 也输出 4,因为 $b 是 $a 的引用,因此也被改变
?>

new 运算符自动返回一个引用,因此对 new 的结果进行引用赋值是错误的。

<?php
class {}

$o = &new C;
?>

以上例程会输出:

Parse error: syntax error, unexpected 'new' (T_NEW) in ...

有关引用更多信息参见本手册中 引用的解释 一章。

算术赋值运算符

例子 等同于 操作
$a += $b $a = $a + $b 加法
$a -= $b $a = $a - $b 减法
$a *= $b $a = $a * $b 乘法
$a /= $b $a = $a / $b 除法
$a %= $b $a = $a % $b 取模
$a **= $b $a = $a ** $b 指数

位赋值运算符

例子 等同于 操作
$a &= $b $a = $a & $b 按位与
$a |= $b $a = $a | $b 按位或
$a ^= $b $a = $a ^ $b 按位异或
$a <<= $b $a = $a << $b 左移
$a >>= $b $a = $a >> $b 右移

其他赋值运算符

例子 等同于 操作
$a .= $b $a = $a . $b 字符串拼接
$a ??= $b $a = $a ?? $b NULL 合并

User Contributed Notes

asc at putc dot de 13-Jul-2015 02:39
PHP uses a temporary variable for combined assign-operators (unlike JavaScript), therefore the left-hand-side (target) gets evaluated last.

Input:
$a += $b + $c;

Meaning:
$a = ($b + $c) + $a;

Not:
$a = $a + ($b + $c);

This can be important if the target gets modified inside the expression.

$a = 0;
$a += (++$a) + (++$a); // yields 5 (instead of 4)
Robert Schneider 13-Mar-2015 01:03
Be aware of assignments with conditionals. The assignment operator is stronger as 'and', 'or' and 'xor'.

<?php
$x
= true and false;   //$x will be true
$y = (true and false); //$y will be false
?>
ma dot bx dot ar at gamil dot com 02-Jun-2014 10:35
Document says:
"An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference in PHP 5. Objects may be explicitly copied via the clone keyword."

But it's not very accurate! Considering this code:
<?php
$a
= new StdClass;
$b = $a;

$a = new StdClass;

var_dump ($a, $b);
?>

Output:
object(stdClass)#2 (0) {
}
object(stdClass)#1 (0) {
}
Note: #2 and #1 means two different objects.

But this code:
<?php
$a
= new StdClass;
$b = &$a;

$a = new StdClass;

var_dump ($a, $b);
?>

Output will be:

object(stdClass)#2 (0) {
}
object(stdClass)#2 (0) {
}

Note: Still pointing to the same object.

And this shows that that exception is not valid, PHP assignment for objects still makes a copy of variable and does not creates a real reference, albeit changing an object variable members will cause both copies to change.
So, I would say assignment operator makes a copy of 'Object reference' not a real object reference.
haubertj at alfredstate dot edu 01-Sep-2011 07:19
[[   Editor's note: You are much better off using the foreach (array_expression as $key => $value) control structure in this case   ]]

When using

<php
while ($var = current($array) {
#do stuff
next($aray)
?>

to process an array, if current($array) happens to be falsy but not === false it will still end the loop.  In such a case strict typing must be used.

Like this:

<php
while (($var = current($array)) !== FALSE) {
#do stuff
next($aray)
?>

Of course if your array may contain actual FALSE values you will have to deal with those some other way.
Peter, Moscow 11-Feb-2011 01:44
Using $text .= "additional text"; instead of $text =  $text ."additional text"; can seriously enhance performance due to memory allocation efficiency.

I reduced execution time from 5 sec to .5 sec (10 times) by simply switching to the first pattern for a loop with 900 iterations over a string $text that reaches 800K by the end.
Hayley Watson 05-Feb-2008 05:54
You could also take adam at gmail dot com's xor-assignment operator and use the fact that it's right-associative:

$a ^= $b ^= $a ^= $b;
Hayley Watson 07-Oct-2007 03:22
bradlis7 at bradlis7 dot com's description is a bit confusing. Here it is rephrased.

<?php
$a
= 'a';
$b = 'b';

$a .= $b .= "foo";

echo
$a,"\n",$b;?>
outputs

abfoo
bfoo

Because the assignment operators are right-associative and evaluate to the result of the assignment
<?php
$a
.= $b .= "foo";
?>
is equivalent to
<?php
$a
.= ($b .= "foo");
?>
and therefore
<?php
$b
.= "foo";
$a .= $b;
?>
bradlis7 at bradlis7 dot com 15-Aug-2005 08:13
Note whenever you do this

<?php
$a
.= $b .= "bla bla";
?>

it comes out to be the same as the following:

<?php
$a
.= $b."bla bla";
$b .= "bla bla";
?>

So $a actually becomes $a and the final $b string. I'm sure it's the same with numerical assignments (+=, *=...).