## Assignment Operators

The 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 right (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\$a = (\$b = 4) + 5; // \$a is equal to 9 now, and \$b has been set to 4.?> ```

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\$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!";?> ```

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.

An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference. Objects may be explicitly copied via the clone keyword.

### Assignment by Reference

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.

``` <?php\$a = 3;\$b = &\$a; // \$b is a reference to \$aprint "\$a\n"; // prints 3print "\$b\n"; // prints 3\$a = 4; // change \$aprint "\$a\n"; // prints 4print "\$b\n"; // prints 4 as well, since \$b is a reference to \$a, which has              // been changed?> ```

The new operator returns a reference automatically, so assigning the result of new by reference is not allowed as of PHP 7.0.0, results in an `E_DEPRECATED` message as of PHP 5.3.0, and an `E_STRICT` message in earlier versions.

For example, this code will result in an error or warning:

``` <?phpclass C {}\$o = &new C;?> ```

Powyższy przykład w PHP 7 wyświetli:

```Parse error: syntax error, unexpected 'new' (T_NEW) in …
```

Powyższy przykład w PHP 5.3 wyświetli:

```Deprecated: Assigning the return value of new by reference is deprecated in …
```

Powyższy przykład w PHP 5 wyświetli:

```Strict Standards: Assigning the return value of new by reference is deprecated in …
```

More information on references and their potential uses can be found in the References Explained section of the manual.

### User Contributed Notes 8 notes

94
Peter, Moscow
11 years ago
``` 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. ```
55
Robert Schneider
7 years ago
``` 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?> ```
29
Hayley Watson
14 years ago
``` 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;?>outputsabfoobfooBecause 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;?> ```
asc at putc dot de
7 years ago
``` 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) ```
-14
ma dot bx dot ar at gamil dot com
8 years ago
``` 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. ```
-15
Hayley Watson
14 years ago
``` 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; ```
-31
``` 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 (+=, *=...). ```
``` [[   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. ```