PHP 8.5: Pipe Operator

Share
PHP 8.5: Pipe Operator
Photo by T K / Unsplash

PHP 8.5 is fast approaching. I wanted to look at some of the features I'm most excited about. One of which is, the pipe operator. This makes it easier for developers who need to chain multiple callables who previously may have done something like this:

$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;

Whilst there's nothing wrong with the above code, it is quite ugly and quite hard to read. Imagine if it was doing double what it's currently doing? It would be even harder to read!

The pipe operator in PHP (|>) will allow us to do the following:

// Using the pipe operator in PHP 8.5
$result = "Hello World"
    |> htmlentities(...)
    |> str_split(...)
    |> fn($x) => array_map(strtoupper(...), $x)
    |> fn($x) => array_filter($x, fn($v) => $v != 'O');

I think this is easier to read and follow what it's doing.

If you want to take a look over the RFC, you can find that below:

PHP: rfc:pipe-operator-v3

If you want to take a look over the Pull Request that implemented this feature, that can be found here:

RFC: Pipe operator by Crell · Pull Request #17118 · php/php-src
cf: https://wiki.php.net/rfc/pipe-operator-v3 Vote has been approved, code seems clean, it should be mergable as soon as CI is happy.