PHP 8.5 Introduces Game-Changing Pipe Operator for Cleaner Code

PHP 8.5 Introduces Game-Changing Pipe Operator for Cleaner Code

PHP 8.5, slated for release later this year, is all set to introduce a highly anticipated feature — the pipe operator (|>). This new addition aims to significantly simplify function chaining and improve code readability, bringing PHP a step closer to functional programming paradigms.

The pipe operator enables developers to pass the result of an expression into the next function in a clean, readable manner. It works similarly to how it’s implemented in languages like Elixir, F#, and JavaScript (with its pipeline proposal).

Traditionally, PHP developers chain functions by nesting them, which can quickly become hard to read, especially with multiple layers of function calls. With the pipe operator, code such as:

$final = array_reverse(array_filter(array_map('trim', $input)));


Can now be rewritten as:

$final = $input
|> array_map('trim', $$)
|> array_filter($$)
|> array_reverse($$);

in this syntax, $$ is a placeholder that gets replaced by the previous expression's result. This small syntactic sugar has the potential to make PHP code not only more elegant but also easier to debug and maintain.


The RFC for this feature was recently accepted and is already generating excitement in the PHP community. While it's still under development, early adopters and framework creators are eager to see how this will influence Laravel, Symfony, and other PHP-based ecosystems.

Posted By: Gurjeet Singh