In this chapter
We'll look at how PHP 8 changed expressions — throw as an expression, the nullsafe ?-> operator, the new rank of the dot, strict rules for nested ternaries and calling methods on new objects without extra brackets — and at PHP 8.5's new pipe operator |>, which chains steps from left to right.
The Problem in Real Life
Customer names arrive from the form with extra spaces and odd capitals: " zoë smith ". Mia cleans them with one line: ucwords(trim($name)). It works, but Marcus asks her to read it out loud in the order things happen. She has to start in the middle — trim first, then ucwords — and read outwards.
"Now imagine five steps," he says, "all nested inside each other. PHP 8.5 added an operator so you can write them in the order they happen. And while we're at it, PHP 8 quietly changed a few other expressions. If you read older tutorials, some of what they say is no longer true."
Why do I have to read this line from the inside out?
Mia
Inside-Out vs. Left-to-Right
Nested calls
f(g(h($x))) runs from the inside out, but we read from the left.
Old tutorials, new rules
PHP 8 changed the dot's rank, nested ternaries and more.
Missing objects
Reading a property of something that might be null used to need an if.
PHP 8 Expression Changes and the Pipe Operator
PHP keeps improving its expressions: since PHP 8.0, several changes made expressions safer or shorter, and PHP 8.5 added a new operator. Here's what you'll meet in modern code:
- PHP 8.0, the dot ranks below + and -:
"Sum: " . 1 + 2now means what it looks like. - PHP 8.0, no unbracketed nested ternaries: the fatal error from chapter 6.
- PHP 8.0, throw as an expression:
$price ?? throw new …. - PHP 8.0, the nullsafe operator ?->: "read this, unless it's null".
- PHP 8.4, new without extra brackets:
new Booking()->label(). - PHP 8.5, the pipe operator |>: pass a value through steps, left to right.
The dot and the plus: before PHP 8, ., + and - had the same rank, so "Sum: " . 1 + 2 was worked out left to right: "Sum: 1" + 2 — an error. PHP 8 lowered the dot's rank, so the maths happens first and the line prints Sum: 3. Still, add brackets — "Sum: " . (1 + 2) — so nobody has to remember which PHP version changed what.
<?phpecho "Sum: " . 1 + 2, "\n"; // Sum: 3 (in PHP 8 and later)echo "Sum: " . (1 + 2), "\n"; // Sum: 3 — clear in every version
throw as an expression: throwing an exception is PHP's way of reporting an error that the code can catch and handle — Act 14 teaches it properly. Before PHP 8, throw was a statement and needed its own line. Now it's an expression, so it can stand on the right of ?? or ?:. $price = $input ?? throw new InvalidArgumentException("Price missing"); reads as "use the input — or, if there is none, stop with this error". You'll use this pattern for values that must be there.
<?php$input = null;try {$price = $input ?? throw new InvalidArgumentException("Price missing");} catch (InvalidArgumentException $e) { // try/catch: Act 14echo "Stopped: ", $e->getMessage(), "\n";}
Output: Stopped: Price missing
The nullsafe operator, ?->: in Act 04 you saw ->, which reads a property of an object: $booking->customer. If $booking might be null, that line would fail. The nullsafe operator ?-> checks first: $booking?->customer gives the customer if $booking is an object, and simply null if it's null — no error. Combined with ??, you get a safe default in one line: $booking?->customer ?? "nobody". You'll use it a lot with objects in Act 11.
<?php$booking = new stdClass();$booking->customer = "Zoë";$noBooking = null;var_dump($booking?->customer); // string(4) "Zoë"var_dump($noBooking?->customer); // NULL — no errorecho $noBooking?->customer ?? "nobody", "\n"; // nobody
new without extra brackets: to create an object and call a method on it straight away, older PHP needed brackets around the new: (new Booking())->label(). Since PHP 8.4, new Booking()->label() works directly. You'll see both forms; they do the same thing.
<?phpclass Booking{public function label(): string{return "Haircut at 10:15";}}echo (new Booking())->label(), "\n"; // the older formecho new Booking()->label(), "\n"; // PHP 8.4 and later
The pipe operator, |>: PHP 8.5's newest operator takes the value on its left and passes it into the function on its right, and the result can be piped on again. $name |> trim(...) |> ucwords(...) means: take $name, trim it, then capitalise the words — written in the order it happens. The trim(...) part, with three dots in the brackets, is called a first-class callable: it refers to the function itself, without calling it yet (Act 09 covers it). Each step must be a function that takes one value; for anything else, pipe into a short arrow function (Act 04, chapter 4), wrapped in brackets.
<?php$name = " zoë smith ";// Inside-out: trim runs first, but is written secondecho ucwords(trim($name)), "\n"; // Zoë Smith// Left to right, in the order it happensecho $name |> trim(...) |> ucwords(...), "\n"; // Zoë Smith// A step that needs more than one value: pipe into an arrow function$label = 2500|> (fn(int $p): int => $p - 500)|> (fn(int $p): string => "£" . number_format($p / 100, 2));echo $label, "\n"; // £20.00
Using pipes well: pipes shine for a chain of three or more transformation steps — cleaning input, formatting a price, preparing data. For one or two steps, a normal call is clearer. And mind the precedence: |> ranks below maths and the dot but above comparisons, so " Tom " |> trim(...) . "!" fails — the dot grabs trim(...) first. Put brackets around a pipe chain whenever it's part of a bigger expression.
| Feature | Since | Example |
|---|---|---|
| Dot ranks below + and - | PHP 8.0 | "Sum: " . 1 + 2 gives "Sum: 3" |
| Nested ternary needs brackets | PHP 8.0 | a ? b : (c ? d : e) |
| throw as an expression | PHP 8.0 | $price ?? throw new Exception(…) |
| Nullsafe operator | PHP 8.0 | $booking?->customer |
| new without extra brackets | PHP 8.4 | new Booking()->label() |
| Pipe operator | PHP 8.5 | $name |> trim(...) |> ucwords(...) |
<?phpvar_dump(1 + 2 |> (fn(int $x): int => $x * 10)); // int(30): 1 + 2 is worked out first// echo " Tom " |> trim(...) . "!";// Error: Object of class Closure could not be converted to stringecho (" Tom " |> trim(...)) . "!", "\n"; // Tom!
Key Takeaway
PHP 8 made the dot rank below + and -, made unbracketed nested ternaries a fatal error, turned throw into an expression ($x ?? throw …), and added the nullsafe ?-> (null instead of an error). PHP 8.4 allows new Foo()->method(). PHP 8.5's pipe |> passes a value through one-argument functions left to right — $name |> trim(...) |> ucwords(...) — so chains read in the order they run.
Why This Matters
BookBay runs PHP 8.5, and the libraries and tutorials Mia will read span many PHP versions. Knowing what changed — and why — tells her which old advice is out of date, and lets her read modern code with ?->, throw expressions and pipes without hesitation.
The pipe operator in particular fits backend work well: cleaning and transforming input step by step is a daily job, and writing the steps in the order they happen makes that code easier to review.
The name cleaner is a tidy pipe now, and Mia has met every operator PHP has. Friday comes round again. Olivia has one request before the weekend: "Rewrite the slot check so that I can read it — and so that it's right." That's the checkpoint.
