The pipe operator is coming to PHP 8.5
PHP 8.5 is set to introduce a new operator called the pipe operator (|>
). This operator allows you to pass the result of one expression as an argument to another expression, making your code cleaner and more readable.
So, today, if you want to pass the result of one function to another, you have to do it like this:
$value = "hello world";
$result = function1(function2(function3($value)));
This can quickly become hard to read, especially with multiple nested functions. Or if you want to simplify the code, you can use variables to store the intermediate results like so:
$value = "hello world";
$result1 = function3($value);
$result2 = function2($result1);
$result = function1($result2);
But this is still not elegant and can make your code look cluttered and hard to follow.
Enter the pipe operator.
- The pipe operator
- The precedence of the pipe operator
- The selection of
|>
operator as syntax - Conclusion
The pipe operator
PHP 8.5 will introduce the pipe operator (|>
), and it will allow you to write the code in a point-free style that limits the use of unnecessary intermediary variables.
So, the above code can be rewritten like this.
$value = "hello world";
$result = $value
|> function3(...)
|> function2(...)
|> function1(...);
As you can tell, the |>
operator, or “pipe”, accepts a single-parameter callable on the right and passes the left-side value to it, evaluating to the callable’s result. Pipe (|>
) evaluates left to right by passing the value (or expression result) on the left as the first and only parameter to the callable on the right.
In the above example, the value of $value
is passed to function3()
, and the result of that is passed to function2()
, and so on. This makes the code much cleaner and easier to read.
Also, to signify that the value is being passed to the function, you can use the ...
operator (First-class Callables) to indicate that the value is being passed as an argument. This is similar to how you would use the ...
operator in a function call.
Here’s a real-world example from the RFC that shows how you can use the pipe operator to transform a string into snake case.
$fullName = 'Fred Flintstone';
$result = $fullName
|> fn($x) => explode(' ', $x) // Produces an array of discrete words
|> fn($x) => implode('_', $x) // Join those words with _
|> strtolower(...) // Lowercase everything
;
// $result === 'fred_flintstone'
Note: The important thing to note here is that the pipe operator is ideal when the functions only accept one argument. If it doesn’t, you can use arrow functions to create a single-argument function to accommodate the pipe operator, just like in the above example.
The precedence of the pipe operator
The pipe operator is left associative, which means that it evaluates from left to right. This means that the left side of the pipe operator is evaluated first, and then the right side is evaluated.
When it’s used in conjunction with comparison operators, the pipe operator binds before the comparison operator.
// The result of the pipe chain is compared against 4.
$res1 = 'beep' |> strlen(...) == 4;
Or this.
$user = $id
|> get_username(...)
?? 'default';
In the above example, the pipe operator is used to get the username from the $id
variable. If the username is not found, it will return default
using the null coalescing operator (??
).
The selection of |>
operator as syntax
It looks like PHP is inspired by the F# programming language and the Elixir programming language when it comes to the pipe operator. The |>
operator is used in both languages to pass the result of one function to another function, the same way as PHP.
In its current form, you might find the pipe operator a bit scattered and disjointed to look at, but with fonts like JetBrains Mono with ligatures, it will make the operator just pleasing to look at.
Conclusion
Although the RFC for the pipe operator is still under the voting phase (at the time of writing this article), it is expected to be accepted and merged into PHP 8.5 since the majority of the votes are in favor of it. And I think this will be a great addition to PHP’s ever-growing list of quality of life features.
Read more about it in the RFC.
👋 Hi there! This is Amit, again. I write articles about all things web development. If you enjoy my work (the articles, the open-source projects, my general demeanour... anything really), consider leaving a tip & supporting the site. Your support is incredibly appreciated!