PHP 8.5 introduces a brand-new feature: the pipe operator (|>).
It allows you to pass the result of one expression directly into the next — just like functional languages (Elixir, F#, JS pipelines, etc.).

This makes your code more readable, more expressive, and perfect for chaining transformations.

Before PHP 8.5

Chaining simple operations required reassigning the same variable multiple times:

$price = 1200;

$price = $price * 0.9;   // apply 10% discount
$price = $price * 1.16;  // add 16% tax
$price = round($price, 2);

Problems:

  • Repetitive $price =

  • Harder to read when multiple transformations are applied

  • Looks imperative, not functional

After PHP 8.5 — Using the Pipe Operator

Now you can transform values step by step, like flowing water through pipes:

$finalPrice = 1200
    |> (fn($p) => $p * 0.9)     // apply 10% discount
    |> (fn($p) => $p * 1.16)    // add 16% tax
    |> (fn($p) => round($p, 2)); // round to 2 decimals

Why this is better?

  • No repeated variable assignments

  • Much more readable

  • Every step expresses one operation

  • Perfect for data transformations, price calculations, sanitizers, formatters…

Another Practical Example

Before

$name = trim($name);
$name = strtolower($name);
$name = ucfirst($name);

After (PHP 8.5)

$cleanName = $name
    |> trim(...)
    |> strtolower(...)
    |> ucfirst(...);

When Should You Use the Pipe Operator?

Use it when you want to:

  • Apply multiple transformations
  • Write expressive, readable code
  • Avoid repeated variable assignments
  • Make logic more functional and clean