PHPAssignment Operators

Assignment Operators

The assignment operator = stores a value in a variable, and you have been using it since the Variables page. PHP also offers a family of compound assignment operators that combine an arithmetic or string operation with assignment, plus a reference-assignment operator that behaves quite differently from ordinary assignment. This page ties all of them together.

Plain assignment

= copies the value on the right into the variable on the left. For scalar values (strings, numbers, booleans), this is always a copy — changing one variable afterward never affects the other.

Assignment copies scalars

PHP
<?php
$a = 5;
$b = $a; // $b gets a copy of 5

$b = 10;
echo $a, " ", $b; // 5 10 - $a is untouched
5 10
Compound assignment operators

Compound operators let you update a variable based on its current value without repeating its name twice. Each one is shorthand for "do this operation, then assign the result back."

Operator

Equivalent to

Example result

$x += $y

$x = $x + $y

add and assign

$x -= $y

$x = $x - $y

subtract and assign

$x *= $y

$x = $x * $y

multiply and assign

$x /= $y

$x = $x / $y

divide and assign

$x %= $y

$x = $x % $y

modulo and assign

$x **= $y

$x = $x ** $y

exponentiate and assign

$x .= $y

$x = $x . $y

concatenate and assign

$x ??= $y

$x = $x ?? $y

assign only if null/unset

Compound assignment in practice

PHP
<?php
$cart_total = 50;
$cart_total += 12.5; // 62.5

$greeting = "Hello";
$greeting .= ", world!"; // "Hello, world!"

$settings = [];
$settings["theme"] ??= "light"; // only sets if not already present
echo $settings["theme"]; // light
light
The null coalescing assignment operator

??=, added in PHP 7.4, is worth calling out on its own because it behaves differently from the others: instead of always applying an operation, it only assigns when the left side is currently null or does not exist yet. It is the standard way to fill in a default value for an array key or a variable that might already be set.

??= only fills in missing values

PHP
<?php
$config = ["timeout" => 30];

$config["timeout"] ??= 60; // stays 30, key already exists
$config["retries"] ??= 3;  // becomes 3, key was missing

print_r($config);
Array
(
    [timeout] => 30
    [retries] => 3
)
Assignment by reference

Ordinary = copies a value. Writing =& instead makes the left variable an alias for the right one — both names now point at the same underlying storage, so changing one changes the other. This is uncommon in everyday code but shows up when passing large arrays into functions without copying them, or when two variables genuinely need to stay in sync.

Reference assignment

PHP
<?php
$original = "draft";
$alias = &$original; // $alias now refers to the same storage

$alias = "published";
echo $original; // published - changed through the alias
published
References can outlive the variable you expect
Once two variables are linked with `=&`, `unset()`-ing one only removes that name — it does not destroy the underlying value if the other variable still references it. Reference assignment also interacts subtly with `foreach` loops: leaving a `&$value` reference from a previous loop active can silently corrupt the last element the next time you loop over the same variable. Unset the loop variable after a by-reference `foreach` to avoid this.
Chained assignment

Because = is itself an expression that evaluates to the assigned value, you can chain several assignments in one statement. PHP evaluates the rightmost assignment first and propagates the value leftward.

Chained assignment

PHP
<?php
$a = $b = $c = 0;
echo "{$a} {$b} {$c}"; // 0 0 0 - all three now hold 0
0 0 0
  • = copies scalar values; =& links two variables to the same storage.

  • Compound operators (+=, .=, etc.) shorten "operate on self, then reassign."

  • ??= is special: it only assigns when the target is null or unset.

  • Chained assignment ($a = $b = 0) works because = returns the assigned value.

Objects and arrays behave differently
Assigning an object variable with `=` copies the reference to the same object (PHP objects are handled internally by handle, not by value), so both variables point at the same instance unless you explicitly `clone` it. Arrays, by contrast, are copied by value on assignment, exactly like the scalars shown above.
Tip
Reach for `??=` whenever you find yourself writing `if (!isset($x)) { $x = $default; }` — it collapses that whole pattern into a single, readable line.