Mastering PHP 8.5 Pipe Operator: Cleaner Code with Step‑by‑Step Examples
The article introduces PHP 8.5's new pipe operator, explains how it passes the result of one expression as the first argument to the next callable, compares it with traditional nested calls, and provides practical code examples and syntax rules for more readable backend code.
The pipe operator is a new feature introduced in PHP 8.5 (released November 20, 2025). Unlike traditional nested function calls or temporary variables, the operator feeds the result of an expression directly as the first argument to the next callable, creating a left‑to‑right flow.
Traditional Approach
Using nested calls requires you to start from the innermost function and work outward:
$donut = bakeDonut();
$donut = addIcing(bakeDonut());
$donut = addSprinkles(addIcing(bakeDonut()));
$donut = addHearts(addSprinkles(addIcing(bakeDonut())));
$donut = addMarshmallow(addHearts(addSprinkles(addIcing(bakeDonut()))));
$donut = addChocolate(addMarshmallow(addHearts(addSprinkles(addIcing(bakeDonut())))));
$donut = addCrumbs(addChocolate(addMarshmallow(addHearts(addSprinkles(addIcing(bakeDonut()))))));Both the traditional and pipe‑based examples produce the same final donut, but the pipe syntax makes the execution order easier to read.
Using the Pipe Operator
$donut = bakeDonut();
$donut = bakeDonut() |> addIcing(...);
$donut = bakeDonut() |> addIcing(...) |> addSprinkles(...);
$donut = bakeDonut() |> addIcing(...) |> addSprinkles(...) |> addHearts(...);
$donut = bakeDonut() |> addIcing(...) |> addSprinkles(...) |> addHearts(...) |> addMarshmallow(...);
$donut = bakeDonut() |> addIcing(...) |> addSprinkles(...) |> addHearts(...) |> addMarshmallow(...) |> addChocolate(...);
$donut = bakeDonut() |> addIcing(...) |> addSprinkles(...) |> addHearts(...) |> addMarshmallow(...) |> addChocolate(...) |> addCrumbs(...);The pipe operator follows a natural reading flow: start with the input and apply each step sequentially, eliminating the cognitive load of matching parentheses.
Real‑World Example
$adminCount = getUsers()
|> fn($users) => array_filter($users, isAdmin(...))
|> count(...);Retrieve all users from the system.
Filter the collection to keep only administrators.
Return the total number of administrators.
Syntax Rules
Each callable must accept exactly one required argument.
Data flows left‑to‑right: A |> B |> C.
The operator works with any valid PHP callable (functions, methods, closures, invokable objects).
The pipe operator integrates with PHP 8.1’s first‑class callable syntax, allowing constructs such as:
$result = "hello world" |> strtoupper(...) |> trim(...);
$data = getUsers()
|> fn($users) => array_filter($users, isAdmin(...))
|> array_values(...)
|> Processor::processUsers(...)
|> $processor->transform(...);First‑class callables create references without invoking the function, which is especially useful in pipe chains.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
