There is not an array-append operator; the reported code must be read as
<?php
$array[] = 'assignment test';
?>
It's simply a special case of array index assignment with the difference that, without to explicitly set an index, PHP will use the last index used and increase it by one.
<?php
$array[1] = 'test';
$array[] = 'another test'; // $array[2]
$array[] = try it again; // $array[3]
?>
赋值运算符
基本的赋值运算符是“=”。一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。
赋值运算表达式的值也就是所赋的值。也就是说,“$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 4 起支持引用赋值,用 $var = &$othervar; 语法,但在 PHP 3 中不可能这样做。引用赋值意味着两个变量都指向同一个数据,没有任何数据的拷贝。有关引用的更多信息见引用的说明。
赋值运算符
Kiam
20-Jun-2009 06:48
20-Jun-2009 06:48
headden at karelia dot ru
09-Jun-2009 11:16
09-Jun-2009 11:16
Pay attention that combined operators affect not only variable value, but also its type:
<?php
$a = "45";
var_dump($a); echo "<br/>";
$a += 5;
var_dump($a); echo "<br/>";
echo "<br/>";
$b = 56;
var_dump($b); echo "<br/>";
$b .= "qweasd";
var_dump($b); echo "<br/>";
?>
Outputs:
string(2) "45"
int(50)
int(56)
string(8) "56qweasd"
Paul Ebermann
29-Apr-2008 01:07
29-Apr-2008 01:07
There is also a array-append-Operator:
<?php
$array []= $element;
?>
This appends the element to the end of the array, as
<?php
array_push($array, $element);
?>
would do.
(This is documented on the array_push page, but not here in the operator section.)
Hayley Watson
06-Feb-2008 01:54
06-Feb-2008 01: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 10:22
07-Oct-2007 10: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;
?>
adam at gmail dot com
25-Aug-2006 05:38
25-Aug-2006 05:38
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$a ^= $b;
bradlis7 at bradlis7 dot com
15-Aug-2005 03:13
15-Aug-2005 03: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 (+=, *=...).
straz at mac dot nospam dot com
21-Feb-2004 06:18
21-Feb-2004 06:18
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
