php version guide
PHP 8.5 Pipe Operator
The PHP 8.5 pipe operator |> passes the expression on its left into a callable on its right. It can make a sequence of transformations read in execution order instead of inside-out.
Changes Worth Recognising
- Use first-class callables such as
trim(...)where the callable accepts the piped value. - Use a short arrow function when an extra argument is required.
- Keep pipelines short enough that error handling and types remain obvious.
- Prefer named steps when transformations need branching or explanation.
Build a Slug
PHP example
<?php
$slug = ' PHP 8.5 Released '
|> trim(...)
|> (fn(string $value): string => str_replace(' ', '-', $value))
|> strtolower(...);
echo $slug . PHP_EOL;
// Prints:
// php-8.5-released
Upgrade Review
- Make sure each callable accepts the previous result.
- Do not hide a complex workflow inside an unreadable chain.
- Confirm deployment runtimes support PHP 8.5 syntax before commit.
Pipelines are most useful for small, linear transformations where each step has an obvious input and output.
Practice
Refactor a Nested Transform
Rewrite strtolower(trim($title)) using the pipe operator. Then add one short transform that replaces spaces with hyphens.
Show solution
Pipe through trim(...), an arrow function calling str_replace(), and strtolower(...). Keep the transformation order visible.