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 $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 |
|---|---|---|
|
| add and assign |
|
| subtract and assign |
|
| multiply and assign |
|
| divide and assign |
|
| modulo and assign |
|
| exponentiate and assign |
|
| concatenate and assign |
|
| assign only if null/unset |
Compound assignment in practice
<?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 $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 $original = "draft"; $alias = &$original; // $alias now refers to the same storage $alias = "published"; echo $original; // published - changed through the alias
published
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
$a = $b = $c = 0;
echo "{$a} {$b} {$c}"; // 0 0 0 - all three now hold 00 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.