The "Pipe" Operator |> in PHP 8.5
Marcos Marcolin • May 18, 2025 • 2 min read
php php85 phprfc opensource🐘 PHP 8.5 will bring a new operator: the Pipe Operator |>.
Yes, an old proposal for the language will finally be implemented, adding a new operator to PHP.
The operator has the following syntax:
<?php
mixed |> callable;
The |> operator, known as pipe, evaluates the expression on the left and passes it as an argument to the function or callable on the right, returning the result of the call.
This means the two code snippets below are logically equivalent:
<?php
// Without Pipe
echo strlen("Hello World"); // 11
// With Pipe
echo "Hello World" |> strlen(...); // 11
// Chaining function calls
// Without Pipe
$input = " Marcos Marcolin ";
$temp = trim($input);
$temp = strtolower($temp);
$temp = ucwords($temp);
echo $temp; // Marcos Marcolin
// With Pipe
echo " Marcos Marcolin "
|> trim(...)
|> strtolower(...)
|> ucwords(...); // Marcos Marcolin
At first glance, it might seem of little use. But when chaining several calls, the pipe operator makes the code much more readable and expressive:
- The left-hand side of the operator can be any value or expression.
- The right-hand side must be a valid PHP callable (function, method, or closure) that accepts only one parameter.
- Functions that require more than one mandatory argument aren't compatible and will raise an error for insufficient arguments.
- If the right-hand side doesn't evaluate to a valid callable, an error will be thrown.
Practical example with string transformation:
<?php
// Without pipe
$temp = "Hello World";
$temp = htmlentities($temp);
$temp = str_split($temp);
$temp = array_map(strtoupper(...), $temp);
$temp = array_filter($temp, fn($v) => $v != 'O');
$result = $temp;
// With pipe
$result = "Hello World"
|> htmlentities(...)
|> str_split(...)
|> fn($x) => array_map(strtoupper(...), $x)
|> fn($x) => array_filter($x, fn($v) => $v != 'O');
Example with pipe on functions and objects:
<?php
function getUsers(): array {
return [
new User('root', isAdmin: true),
new User('marcos.marcolin', isAdmin: false),
];
}
function isAdmin(User $user): bool {
return $user->isAdmin;
}
// This is the new syntax.
$numberOfAdmins = getUsers()
|> fn ($list) => array_filter($list, isAdmin(...))
|> count(...);
var_dump($numberOfAdmins); // int(1);
At first, the syntax might sound a bit strange to those used to traditional operators. But once you understand the proposal, it turns out to be an elegant and powerful addition to the language.
You can read more about the RFC in the official post: click here.