PHP continues to evolve toward more expressive and readable code. One of the most anticipated features in PHP 8.5 is the Pipe Operator (|>), which enables a clean, functional programming style by passing the result of one expression directly into another with PHP Pipe Operator.
🔹 What is the Pipe Operator?
The pipe operator (|>) allows you to take the result of an expression and “pipe” it into a function as its input.
Syntax:
$result = value |> function;
This is equivalent to:
$result = function(value);
So what is the problem the Pipe operator solves?
Consider this traditional example where you take the result of a function as the input to other function, typically when working with strings:
$result = trim(strtoupper(substr($text, 0, 10)));
Most of us wrote something like this, and this becomes harder to read with more nesting in function calls.
With pipes the same logic becomes more readable:
$text = "Hello World";
$result = $text
|> substr(..., 0, 5)
|> strtoupper(...)
|> trim(...);
So what is the ... operator in here, it tells the pipe operator to take the previous output and pass it directly to the function.
If you need to pass the argument by name to the next function call, you can use a function like arrow function on subsequent calls like so:
$text = "Hello World";
$result = $text
|> fn(string $string) => substr($string, 0, 5)
|> fn(string $string) => strtoupper($string)
|> trim(...);
Let’s see some examples:
Simple Function Chaining
function double($x) {
return $x * 2;
}
function addThree($x) {
return $x + 3;
}
$result = 5
|> double(...)
|> addThree(...);
echo $result; // 13
With Strings
$text = " hello world ";
$result = $text
|> trim(...)
|> strtoupper(...);
echo $result; // "HELLO WORLD"
Using built-in functions
$result = "php pipe operator"
|> fn(string $string) => ucwords($string)
|> str_replace(" ", "-", ...);
echo $result; // "Php-Pipe-Operator"
Placeholder Syntax (...)
The ... acts as a placeholder for the piped value.
$value |> function_name(...);
You can also place it anywhere in the argument list:
$result = 5 |> pow(2, ...); // 2^5 = 32
Multiple Arguments Example
function multiply($a, $b) {
return $a * $b;
}
$result = 5 |> multiply(10, ...);
echo $result; // 50
Using Anonymous Functions
$result = 10
|> (fn($x) => $x + 5)
|> (fn($x) => $x * 2);
echo $result; // 30
Array Processing Example
numbers = [1, 2, 3, 4, 5];
$result = $numbers
|> array_map(fn($n) => $n * 2, ...)
|> array_filter(fn($n) => $n > 5, ...);
print_r($result); // [6, 8, 10]
Error Handling Example
$result = "123"
|> intval(...)
|> (fn($x) => $x > 100 ? $x : throw new Exception("Too small"));
Conclusion
The Pipe Operator in PHP 8.5 is a powerful addition that modernizes PHP and brings it closer to functional programming languages. It simplifies complex expressions, improves readability, and helps developers write more maintainable code.


