In this chapter
We'll look at how names connect to values: variable variables, where a variable's name comes from another variable; references, where two names share one value; and why a function that changed a number didn't change it for the code that called it.
The Problem in Real Life
In the freelancer's old report.php, Mia finds a line with two dollar signs: $$field = $value;. A few lines later, the code uses a variable called $price that is never assigned anywhere. Yet it works.
Back in her own code, she writes a small function, addBooking(), that adds 1 to the booking count. She calls it three times, and inside the function she even prints the new number — 8, then 8, then 8. After the calls, $bookings is still 7.
"Both puzzles have the same answer," says Marcus. "It's about how a name connects to a value — and when PHP gives you the value itself, and when it gives you a copy."
My function changed the number — I saw it — so why is it still 7 outside?
Mia
A Copy of the Value vs. the Value Itself
Two dollar signs
Old code uses $$field, and nobody can tell which variable it really means.
Changes that disappear
A function works on a copy, so its changes vanish when it ends.
Two names, one value
PHP can make two names share one value — useful, but surprising when you don't expect it.
Variable Variables and References
This chapter is about how a name connects to a value. In chapter 1 you learned that assignment copies: after $b = $a; there are two separate values. Most of the time that's exactly what you want. But you'll meet two other ideas in real PHP code:
- Variable variables: a variable whose name is taken from the value of another variable.
- References: two names that share one value, so a change made through one name is seen through the other.
Variable variables: $$field means "take the value of $field, and use it as the name of a variable". If $field holds the text "price", then $$field is the variable $price. That's how the freelancer's code created $price without ever writing its name. You can also build the name from an expression inside braces: ${"pri" . "ce"}. The code below shows both.
<?php$field = "price";$$field = 25; // the same as: $price = 25;echo $price, "\n"; // 25echo ${"pri" . "ce"}, "\n"; // 25: the name is built from an expression
Why you should avoid them: variable variables work, but they make code very hard to follow. Nobody — not you, not your editor, not a tool like PHPStan — can tell which variable $$field means without running the code, and a wrong value quietly creates yet another variable. Whenever you feel you need them, what you really need is a list with named items — an array, like $prices["haircut"] — which you'll learn in Act 08. Recognise $$ in old code; don't write it in new code.
<?php$prices = ["haircut" => 25, "beardTrim" => 10]; // an array (Act 08)$service = "haircut";echo $prices[$service]; // 25: clear, and easy for tools to check
References: a reference is a second name for the same value. You create one by putting an ampersand & in front of the variable on the right: $alias = &$todayCount;. Now the two names are like two labels stuck on one box. Change the value through either name, and both see the change. The diagram compares a copy with a reference, and the code shows both. Removing one name with unset($alias) only removes that label — the value and the other name stay.
A copy vs. a reference
$copy = $todayCount;
two boxes: 3 and 3
$alias = &$todayCount;
one box, two labels
$copy = 10;
$todayCount is still 3
$alias = 10;
$todayCount is now 10 too
<?php$todayCount = 3;$copy = $todayCount; // a copy$alias = &$todayCount; // a reference: a second name for the same value$alias = 10;echo "todayCount: $todayCount\n"; // 10: changed through $aliasecho "copy: $copy\n"; // 3: the copy didn't changeunset($alias); // removes the name, not the valueecho "todayCount: $todayCount\n"; // still 10
Functions get copies: a quick reminder from Act 02, chapter 6: a function is a named piece of code you run by calling its name, and it receives values through its parameters — the variables in its brackets. When you call a function with a variable, PHP gives the function a copy of the value. The function can change its copy as much as it likes; the caller's variable stays the same. That's Mia's bug, in the first code box below: addBooking() added 1 to its own copy, which was thrown away when the function ended. This is called passing by value. It's PHP's normal behaviour, and a good one: a function can't change your variables behind your back.
<?phpfunction addBooking(int $count): void{$count = $count + 1;echo "Inside: $count\n"; // Inside: 8}$bookings = 7;addBooking($bookings);echo "Outside: $bookings\n"; // Outside: 7
Passing by reference: if a function really should change the caller's variable, put & before the parameter: function addBooking(int &$count). Now the parameter is a reference to the caller's variable, not a copy. This is called passing by reference. Some of PHP's own functions work this way — for example, sort() sorts the very list you give it (Act 08). For your own functions, though, the clearer way is almost always to return the new value and assign it: $bookings = withOneMoreBooking($bookings);. Then anyone reading the call can see that $bookings changes.
<?phpfunction addBooking(int &$count): void // & before the parameter{$count = $count + 1;}$bookings = 7;addBooking($bookings);echo $bookings, "\n"; // 8
<?phpfunction withOneMoreBooking(int $count): int{return $count + 1;}$bookings = 7;$bookings = withOneMoreBooking($bookings); // you can see $bookings change hereecho $bookings, "\n"; // 8
Returning by reference: a function can also hand back a reference instead of a copy. It's written with & before the function's name: function &findCounter(). The caller then gets a second name for a value the function owns. This is rare and mostly used inside frameworks and libraries; you won't need to write one in this course. Just recognise the & before a function name when you see it.
When to use references: at the start, almost never. They make code harder to follow, because a value can change from a place you weren't looking at. You'll meet them in two places later: changing every item of a list inside a foreach loop (Act 08), and objects, which have their own sharing rules (Act 11). The table below sums up the four ways a name can get a value.
| Code | What happens | Does a change show in the other? |
|---|---|---|
| $b = $a; | $b gets a copy | No |
| $b = &$a; | $b becomes a second name for $a's value | Yes |
| addBooking($count) | The function gets a copy | No |
| function addBooking(int &$count) | The function works on the caller's variable | Yes |
Key Takeaway
$$name uses the value of $name as a variable name — it works, but use arrays instead. $b = &$a makes two names share one value, so a change through one is seen through the other. Functions get copies of their arguments (passing by value); &$param passes by reference so the function can change the caller's variable — but returning the new value is usually clearer.
Why This Matters
Knowing that functions get copies explains a whole family of "my change disappeared" bugs — and it's also why BookBay's code can trust that calling a function won't secretly change its variables. When a function does need to change something, Mia now knows the two ways, and why returning a value is usually the better one.
And reading old code is part of every developer's job. $$field and &$count will turn up in older PHP projects and in PHP's own functions; recognising them, and knowing when not to copy the pattern, is part of reading PHP fluently.
Mia rewrites addBooking() to return the new count, and the report adds up. Feeling confident, she moves the price calculation into a function too, priceWithFee(). The booking fee, $bookingFee = 2;, sits right at the top of the file. But the page now shows £25 instead of £27, with a warning: Undefined variable $bookingFee. "It's right there, three lines up! Can't the function see it?" That's the last chapter of this Act.
