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.)
代入演算子
代入演算子の基本となるものは "=" です。この演算子に関して最初に 思い付く意味は"等しい"であるかもしれません。しかし、そうではありません。 本当は、左オペランドに右オペランドの式の値を設定する("得て代入する") ことを意味します。
代入式の値は、代入される値です。つまり、"$a = 3" の値は、3 です。 これにより、以下のようなトリッキーなことができるようになります。
<?php
$a = ($b = 4) + 5; // $a は 9 に等しく、$b は 4 にセットされます。
?>
基本代入演算子に加えて、全ての バイナリ演算子、配列結合および文字列演算子に関して 「複合演算子」があります。 これにより、式の中の値を使用し、その値をその式の結果とすることができます。 例えば、
<?php
$a = 3;
$a += 5; // $a を 8 にセットします。$a = $a + 5; と同じです。
$b = "Hello ";
$b .= "There!"; // $bを"Hello There!"にセットします。$b = $b . "There!";と同じです。
?>
代入は、元の変数を新しい変数にコピーする(値による代入)ため、 片方の変数に対する変更はもう片方に影響を与えないということに 注意してください。この動作により、密なループの内側で大きな配列のようなものを コピーする必要がある場合には問題を生じる可能性があります。 $var = &$othervar; 構文を用いた、参照による代入もサポートしています。 '参照による代入'は、両方の変数が同じデータを指し、コピーを 行わないことを意味します。参照に関する詳細については、 リファレンスの説明も 参照ください。 PHP 5 では、新しいキーワード clone を使用して明示的に指定しない限り、オブジェクトは自動的に参照による代入になります。
代入演算子
Paul Ebermann
29-Apr-2008 10:07
29-Apr-2008 10:07
Hayley Watson
06-Feb-2008 10:54
06-Feb-2008 10: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
08-Oct-2007 07:22
08-Oct-2007 07: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
26-Aug-2006 02:38
26-Aug-2006 02:38
or you could use the xor-assignment operator..
$a ^= $b;
$b ^= $a;
$a ^= $b;
bradlis7 at bradlis7 dot com
16-Aug-2005 12:13
16-Aug-2005 12: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 03:18
21-Feb-2004 03: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
