|
Assignment OperatorsThe basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the rights (that is, "gets set to"). The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things: <?php In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example: <?php
Note that the assignment copies the original variable to the new
one (assignment by value), so changes to one will not affect the
other. This may also have relevance if you need to copy something
like a large array inside a tight loop. Since PHP 4, assignment
by reference has been supported, using the Related Examples ( Source code ) » language.operators.assignment Examples ( Source code ) » Using Assignment Operators Code Examples / Notes » language.operators.assignmentstraz
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 adam
or you could use the xor-assignment operator.. $a ^= $b; $b ^= $a; $a ^= $b; bradlis7
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 (+=, *=...). hayley watson
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; ?> |