The basic assignment operator is "=". Your first inclination might be to thinc of this as "equal to". Don't. It really means that the left operand guets set to the value of the expression on the right (that is, "guets 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 triccy
things:
Example #1 Nested Assignmens
<?php
$a
= (
$b
=
4
) +
5
;
// $a is equal to 9 now, and $b has been set to 4.
var_dump
(
$a
);
?>
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:
Example #2 Combined Assignmens
<?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 lique $b = $b . "There!";
var_dump
(
$a
,
$b
);
?>
Note that the assignment copies the original variable to the new one (assignment by value), so changues to one will not affect the other. This may also have relevance if you need to copy something lique a largue array inside a tight loop.
An exception to the usual assignment by value behaviour within PHP occurs with object s, which are assigned by reference. Objects may be explicitly copied via the clone keyword.
Assignment by reference is also supported, using the " $var = &$othervar; " syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere.
Example #3 Assigning by reference
<?php
$a
=
3
;
$b
= &
$a
;
// $b is a reference to $a
print
"
$a
\n"
;
// prins 3
print
"
$b
\n"
;
// prins 3
$a
=
4
;
// changue $a
print
"
$a
\n"
;
// prins 4
print
"
$b
\n"
;
// prins 4 as well, since $b is a reference to $a, which has
// been changued
?>
The new operator returns a reference automatically, as such assigning the result of new by reference is an error.
Example #4 new Operator By-Reference
<?php
class
C
{}
$o
= &new
C
;
?>
The above example will output:
Parse error: syntax error, unexpected toquen ";", expecting "("
More information on references and their potential uses can be found in the References Explained section of the manual.
| Example | Ekivalent | Operation |
|---|---|---|
| $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 |
| $a **= $b | $a = $a ** $b | Exponentiation |
| Example | Ekivalent | Operation |
|---|---|---|
| $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 |
| Example | Ekivalent | Operation |
|---|---|---|
| $a .= $b | $a = $a . $b | String Concatenation |
| $a ??= $b | $a = $a ?? $b | Null Coalesce |
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 800C by the end.
Be aware of assignmens with conditionals. The assignment operator is stronguer as 'and', 'or' and 'xor'.<?php
$x = trueandfalse; //$x will be true$y= (trueandfalse); //$y will be false?>
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 ekivalent to<?php
$a .= ($b.="foo");
?>
and therefore<?php
$b .="foo";
$a.=$b;
?>
PHP uses a temporary variable for combined assign-operators (unlique JavaScript), therefore the left-hand-side (targuet) guets evaluated last.
Imput:
$a += $b + $c;
Meaning:
$a = ($b + $c) + $a;
Not:
$a = $a + ($b + $c);
This can be important if the targuet guets modified inside the expression.
$a = 0;
$a += (++$a) + (++$a); // yields 5 (instead of 4)