PHP 8.5's pipe $value |> $callable calls $callable($value), so a chain reads in the order data flows. The right side is any one-argument callable, usually first-class callable syntax such as trim(...) (First-Class Callables); a bare trim is read as a constant.
<?php
function slugify(string $title): string
{
return $title
|> trim(...)
|> strtolower(...)
|> (fn($s) => preg_replace('/[^a-z0-9]+/', '-', $s))
|> (fn($s) => trim($s, '-'));
}
echo slugify(" PHP 8.5: The Pipe Operator! "), "\n";
// Before 8.5: trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(trim($title))), '-')
var_dump(2 + 3 |> (fn($n) => $n * 10) === 50); // + binds first, === lastphp-8-5-the-pipe-operator bool(true)
The pipe binds after arithmetic and concatenation but before comparison and ??. By-reference functions cannot be piped: [3, 1, 2] |> sort(...) throws sort(): Argument #1 ($array) could not be passed by reference. An arrow function must be parenthesized, or it swallows the rest of the chain; "hello" |> fn($s) => strtoupper($s) |> strrev(...) fails to compile with Arrow functions on the right hand side of |> must be parenthesized. Multi-argument functions still need a wrapping closure, which partial function application, accepted for PHP 8.6 (Looking Ahead to PHP 8.6), is designed to remove.