Ehsan Bagherzadegan
Back to articles
5 min read

PHP 8.5 Pipe Operator: Cleaner Data Transformations

PHP 8.5 adds the |> pipe operator, which turns nested function calls into a top-to-bottom flow. Here is how it works, the syntax rules, and when collections or Laravel's Pipeline are still the better tool.

Most PHP code that transforms a value ends up in one of two shapes: deeply nested function calls read from the inside out, or a chain of temporary variables. PHP 8.5 adds a third option, the pipe operator |>. It passes the result of the left side into the callable on the right, so a transformation reads top to bottom.

This article covers how it works, where it helps, and where it does not.

The basic idea

Here is a slug helper written the traditional, nested way:

$slug = trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($title)), '-');

You have to start reading at $title and work outwards. With the pipe operator the same logic reads in execution order:

$slug = $title
    |> strtolower(...)
    |> (fn (string $s) => preg_replace('/[^a-z0-9]+/', '-', $s))
    |> (fn (string $s) => trim($s, '-'));

Each step receives the previous result as its single argument. The strtolower(...) syntax is first-class callable syntax, which has existed since PHP 8.1. The pipe operator builds on it.

Rules to remember

  • The right-hand side must be a callable, and it is called with exactly one argument: the value from the left.

  • Arrow functions and other closures must be wrapped in parentheses, as in the example above. Without them the parser will not accept the expression.

  • Functions that need more than one required argument cannot be used directly. Wrap them in a closure or create a small named function.

  • The operator is left-associative, so a chain is evaluated from top to bottom.

The parentheses rule is the one that surprises people most. If you get a syntax error on a pipe line, check that first.

A more realistic example

Imagine cleaning up a list of email addresses from a CSV import. First, small named functions make each step testable:

function normaliseEmails(array $rows): array
{
    return array_map(fn (string $e) => strtolower(trim($e)), $rows);
}

function onlyValid(array $emails): array
{
    return array_filter($emails, fn (string $e) => filter_var($e, FILTER_VALIDATE_EMAIL) !== false);
}

function unique(array $emails): array
{
    return array_values(array_unique($emails));
}

Then the pipeline shows the intent without noise:

$emails = $rawEmails
    |> normaliseEmails(...)
    |> onlyValid(...)
    |> unique(...);

I like this style because the pipeline reads like a list of business steps, and each step is a plain function you can unit test on its own.

Pipes versus Laravel collections

Laravel developers already have a fluent way to chain transformations: collections. So is the pipe operator redundant for us?

Not really, but the overlap is real. Collections work well when you are transforming a list of items with map, filter and groupBy. The pipe operator works on any value: a string, an integer, a DTO, a request payload. It also needs no wrapper object and no framework.

SituationBetter fit
Mapping or filtering many itemsCollection
Several string operations on one valuePipe operator
Framework-free library codePipe operator
Multi-step process with side effects, branching or early exitLaravel Pipeline or an Action class

That last row matters. Laravel's Pipeline class passes an object through middleware-style stages, and each stage decides whether to continue. The |> operator has no such control. It is plain function application, nothing more.

Gotchas

Error handling stays the same. If a step throws, the exception propagates as usual. There is no short-circuit for null. If one step returns null and the next expects a string, you get a TypeError under strict types. Keep steps total, or validate before the chain.

Debugging is slightly harder. There is no variable to inspect between steps. A tiny helper that dumps and returns its input solves this:

function tap_dump(mixed $value): mixed
{
    var_dump($value);
    return $value;
}

$result = $input
    |> normaliseEmails(...)
    |> tap_dump(...)
    |> onlyValid(...);

Do not over-use it. A two-step transformation is often clearer as two ordinary statements. If a pipeline needs many inline closures, extract them into named functions or reconsider the design.

Check static analysis support. Make sure your PHPStan, Rector and IDE versions understand PHP 8.5 syntax before you adopt it in a shared codebase. Also confirm that your production runtime is on PHP 8.5; the syntax is a parse error on older versions.

Other small additions in PHP 8.5

The same release also brought a few smaller conveniences worth a look, such as array_first() and array_last(), which replace the usual reset() and end() tricks or $array[array_key_first($array)] for getting the first and last value of an array. Check the official release notes for the complete list.

When I would use it

I would reach for the pipe operator in pure, value-in, value-out code: formatters, normalisers, parsers and small domain helpers. I would not use it to replace collections in Eloquent-heavy code, and I would not use it for workflows that need branching or side effects. There, an Action class or Laravel's Pipeline stays the clearer tool.

Takeaway

The pipe operator is a small feature with a narrow, useful scope. It turns nested calls into a readable top-to-bottom flow, works with any callable, and costs nothing at runtime beyond the calls themselves. Try it on one helper class first and see whether your team finds it easier to read.

Sources