← All posts

PHP 8.5 Pipe Operator |>: When Chaining Helps (and When a Temp Variable Wins)

PHP 8.5's |> passes a value into the next single-arg callable. When pipe chains help business code, when named temps stay clearer, and the rules that matter.

Ian at a desk with a laptop, brown tabby cat beside him, soft natural light

Nested calls are honest about one thing: they hate being read. The value starts in the middle, the next step sits somewhere to the left, and by the third wrap you've lost the plot.

Temporary variables fix that, but they add noise when every name is $temp2. PHP 8.5 offers a third option: the pipe operator, |>.

What the operator does

Per the PHP manual on functional operators, |> accepts a single-parameter callable on the right and passes the left-hand value into it. The right-hand side may be a Closure, a first-class callable such as strlen(...), an invokable object, or an expression that evaluates to one of those.

A one-step pipe is equivalent to a normal call:

"Hello World" |> strlen(...);

That spelling is not especially useful on its own. The payoff appears when you chain steps that would otherwise nest or force a row of throwaway variables.

The PHP Foundation write-up and the pipe operator v3 RFC both stress the same point: the win is readable pipelines, not clever one-liners.

A short chain (from the docs)

The manual's chaining example is the right shape for everyday transforms — encode, split, map, filter — without inventing run output:

``` $result = "PHP Rocks" |> htmlentities(...) |> str_split(...) |> (fn($x) => array_map(strtoupper(...), $x)) |> (fn($x) => array_filter($x, fn($v) => $v != 'O')); ```

Each line does one job. That is easier to review in a pull request than array_filter(array_map(strtoupper(...), str_split(htmlentities(...)))). The left-to-right order matches how you explain the process to a colleague.

Rules that stop the surprises

  • Arrow functions must be wrapped in parentheses on the right of a pipe. Write (fn($x) => array_map(strtoupper(...), $x)). Miss the brackets and you get a fatal error. That is syntax ambiguity, not optional style.
  • Right-hand callables may take only one required parameter. Functions that need more arguments are not allowed as-is. Wrap the multi-arg call in a one-arg Closure.
  • By-reference parameters are not allowed on the right-hand side. Pipe is for value flow, not shared mutation that jumps out of the chain.
  • If the right-hand side is not a valid callable, PHP throws an Error.

Those limits keep the chain predictable. That is what you want in booking, invoice or catalogue code.

When a plain variable is clearer

Use a pipe when the steps are short, obvious and you want a single expression — for example inside a match arm, a short Closure, or a property hook getter.

Prefer temporary variables when:

  • a step needs a meaningful name for the next reader
  • you expect to dump or log mid-pipeline while debugging
  • the chain has grown long enough that you are squinting

A single call should stay a single call. strlen($name) beats a ceremonial pipe every day of the week.

The temp-variable equivalent of the chain above is still valid PHP and still fine code:

``` $temp = "PHP Rocks"; $temp = htmlentities($temp); $temp = str_split($temp); $temp = array_map(strtoupper(...), $temp); $temp = array_filter($temp, fn($v) => $v != 'O'); $result = $temp; ```

Same steps. Easier breakpoints. Clarity is not a failure mode.

Fit with the rest of a tidy PHP 8.5 codebase

Pipe sits beside other habits that shrink busywork without changing your domain model. If you are already using clone with for readonly value objects, the same rule of thumb applies: adopt the feature where it removes noise, leave it alone where it adds theatre.

Check the runtime first. Our PHP 8.2 end-of-life guide is still the place to start if you are not sure what the server is running. And keep declare(strict_types=1); at the top of new files — pipes do not replace honest types at the method boundary.

For small business sites, the practical test is simple. Can the next person follow the transform without opening three tabs? If yes, ship it. If not, name the steps.

Where to go next

A lighter take on the same feature is on Ian's WordPress blog.

Readonly objects, enums, match, strict types and tested examples run through PHP Done The Correct Way. The companion code page has the downloads, and the book page has the details.

If it makes money, it makes sense. A transform you can still read next quarter is a good place to start.

Let's talk More posts