References
A reference in PHP makes two names point at one value, and the ampersand is the sign of it. It is a sharp tool: the page shows where it earns its place and how the loop case goes wrong.
Assign by reference
Ampersand links two names to one value. Writing through either name updates the shared slot.
Objects already share state without this trick, so plain variables are the main case for explicit links.
<?php $a = 10; $b = &$a; $b = 20; echo $a, PHP_EOL; echo $b, PHP_EOL;
php assign.php
20
20
Only $b was written, yet both names print 20, because they never held two values.
Loop by reference + unset
A foreach value with ampersand edits the array in place. Each turn writes straight into the source slot.
Always unset the loop variable right after such a loop so later reuse does not overwrite the last item by accident.
<?php $nums = [1, 2, 3]; foreach ($nums as &$v) { $v = $v * 2; } unset($v); echo implode(", ", $nums), PHP_EOL; $second = [1, 2]; foreach ($second as &$v) { } $v = 99; echo implode(", ", $second), PHP_EOL;
php foreach.php
2, 4, 6
1, 99
The first loop doubles every item and the unset() ends its link. The second loop has no unset, so the later assignment to $v still writes into the array and turns the last item into 99: that is the accident the sentence above warns about.
Return by reference
A function with ampersand in its signature can return a slot instead of a copy. Callers then bind and update the original through the result.
Both sides need ampersand: one on the function and one on the receiving assign.
<?php $store = ["n" => 1]; function &getSlot(array &$row): int { return $row["n"]; } $slot = &getSlot($store); $slot = 42; echo $store["n"], PHP_EOL; echo $slot, PHP_EOL;
php return.php
42
42
The assignment through $slot reached the array inside the function, and the array argument is passed by reference as well, so the caller's $store changed.
Avoid mostly
References add hidden links that are hard to trace. Most tasks read better with plain returns and fresh arrays.
Reach for return values first and reserve reference tricks for tight loops and small helpers only.
<?php $nums = [1, 2, 3]; $doubled = array_map(fn($v) => $v * 2, $nums); echo implode(", ", $doubled), PHP_EOL; echo implode(", ", $nums), PHP_EOL;
php avoid.php
2, 4, 6
1, 2, 3
The map returns a new array and leaves the input as it was, so nothing is shared behind the reader's back.
Exercises
- Link two variables with ampersand, unset one of them and show what the other still holds.
- Double an array of prices in a by-reference foreach and print it.
- Remove every empty value from an array with a by-reference loop and unset().
- Write a function that returns a reference to the first element of an array and update it.
- Rewrite the same update as a function that returns a new array and compare the two versions.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
$b = &$a;do?Answer
It makes both names point at one value
The ampersand binds the name to the same slot, so a write through either name is visible in both.
-
Why call
unset($v)after a by-reference foreach loop?Answer
To keep the last item from changing through the leftover link
After the loop the variable is still a reference to the last element, and any write to it edits the array.
-
What does a function need to return a reference?
Answer
An ampersand before the function name
The ampersand in the signature declares the reference, and the caller needs one too to bind it.
-
Why does the lesson suggest avoiding references in ordinary code?
Answer
They create hidden links that are hard to follow
A plain return value shows the data flow, while a reference changes a variable somewhere else.
-
What does this program print?
<?php $a = 10; $b = &$a; $b = 20; echo $a;Answer
20
The write went through the shared slot, so the original name shows the new value too.
-
What does this program print?
<?php $nums = [1, 2, 3]; foreach ($nums as &$v) { $v = $v * 2; } unset($v); echo implode(", ", $nums);Answer
2, 4, 6
Each turn writes the doubled value back into the array, so all three items change.
-
What does this program print?
<?php $second = [1, 2]; foreach ($second as &$v) { } $v = 99; echo implode(", ", $second);Answer
1, 99
Without unset the loop variable still points at the last element, and 99 lands there.
-
What does this program print?
<?php $store = ["n" => 1]; function &getSlot(array &$row): int { return $row["n"]; } $slot = &getSlot($store); $slot = 42; echo $store["n"];Answer
42
The returned reference points into the array, so writing through the new name changes the original.
-
What does this program print?
<?php $nums = [1, 2, 3]; $doubled = array_map(fn($v) => $v * 2, $nums); echo implode(", ", $nums);Answer
1, 2, 3
array_map() builds a new array and never edits its input, which is the alternative the last section recommends.
-
What does
$vhold afterforeach ($items as $v) {}ends when the loop is by value?Answer
A copy of the last item
Without the ampersand the value is copied each turn, so the variable keeps the last copy and no link.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev