©
本文檔使用 php中文網手册 發布
基本的赋值运算符是“=”。一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。
赋值运算表达式的值也就是所赋的值。也就是说,“$a = 3”的值是 3。这样就可以做一些小技巧:
<?php
$a = ( $b = 4 ) + 5 ; // $a 现在成了 9,而 $b 成了 4。
?>
对于数组 array ,对有名字的键赋值是用“=>”运算符。此运算符的优先级和其它赋值运算符相同。
在基本赋值运算符之外,还有适合于所有二元算术,数组集合和字符串运算符的“组合运算符”,这样可以在一个表达式中使用它的值并把表达式的结果赋给它,例如:
<?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 的引用,因此也被改变
?>
自 PHP 5 起,new
运算符自动返回一个引用,因此再对
new
的结果进行引用赋值在 PHP 5.3 以及以后版本中会发出一条
E_DEPRECATED
错误信息,在之前版本会发出一条
E_STRICT
错误信息。
例如以下代码将产生警告:
<?php
class C {}
$o = &new C ;
?>
有关引用更多信息参见本手册中引用的解释一章。
[#1] asc at putc dot de [2015-07-13 14:39:29]
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)
[#2] Robert Schneider [2015-03-13 13:03:32]
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
?>
[#3] ma dot bx dot ar at gamil dot com [2014-06-02 10:35:31]
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.
[#4] haubertj at alfredstate dot edu [2011-09-01 07:19:40]
[[ 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.
[#5] Peter, Moscow [2011-02-11 01:44:54]
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.
[#6] Hayley Watson [2008-02-05 17:54:11]
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;
[#7] Hayley Watson [2007-10-07 15:22:17]
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;
?>
[#8] bradlis7 at bradlis7 dot com [2005-08-15 08:13:01]
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 (+=, *=...).
[#9] straz at mac dot nospam dot com [2004-02-20 22:18:19]
This page really ought to have table of assignment operators,
namely,
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