PHP 8.5 introduces the pipe operator for composing calls without nested expressions.
What It Does and When to Use It
The right side is a callable that receives the left expression as its only argument. A minimum platform requirement controls what syntax can be parsed; target-version tests find runtime and extension incompatibilities.
Requirements, Inputs and Expected Behavior
The code using the pipe operator, NoDiscard, or void cast requires PHP 8.5 or later; code with older syntax can still run on earlier supported PHP releases. The operator requires PHP 8.5 and a callable on the right; it is not the Unix shell pipe.
Example 1: Basic Operation
The right side is a callable that receives the left expression as its only argument.
<?php
$result = ' PHP 8.5 ' |> trim(...) |> strtoupper(...); echo $result;
Example 2: Behavior and Validation
A first-class callable accepts the single value emitted by the previous stage.
<?php
function addTax(float $amount): float {return round($amount*1.17,2);}
$invoiceTotal = [19.90, 5.10] |> array_sum(...) |> addTax(...); echo $invoiceTotal;
Practical Example: Application Use
A closure can adapt the pipeline to a function that needs extra fixed arguments.
<?php
$parts = ' draft:ready ' |> trim(...) |> (fn(string $value): array => explode(':',$value,2));
if (count($parts)!==2) throw new InvalidArgumentException('Invalid state'); print_r($parts);
Important Behavior and Edge Cases
The second example highlights: A first-class callable accepts the single value emitted by the previous stage. The practical example adds: A closure can adapt the pipeline to a function that needs extra fixed arguments.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| Minimum PHP version and target PHP version | A minimum platform requirement controls what syntax can be parsed; target-version tests find runtime and extension incompatibilities. |
| Choosing safely | Do not introduce syntax from a newer minor version before every production host supports it. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming newer syntax parses on older PHP | Lint with the oldest supported runtime; gate or delay newer syntax. |
| Upgrading CLI PHP but not the web worker | Check the FPM binary, all extensions and the Composer platform in staging. |
| Skipping operational constraints | The operator requires PHP 8.5 and a callable on the right; it is not the Unix shell pipe. |
Key Takeaways
- PHP 8.5 introduces the pipe operator for composing calls without nested expressions.
- A closure can adapt the pipeline to a function that needs extra fixed arguments.
- Do not introduce syntax from a newer minor version before every production host supports it.