PHP 8.5 New Feature: The Pipe Operator (|>) — Simplifying Function Chaining

The Pipe Operator

PHP 8.5 introduces one of the most awaited and modern syntax features — the Pipe Operator (|>).

This new operator simplifies function chaining, data transformation, and makes code more readable and declarative, much like what developers already enjoy in languages such as JavaScript, Elixir, and Hack.

 

What Is the Pipe Operator?

The pipe operator (|>) allows you to pass the result of one expression directly as an argument to another function — without nesting multiple function calls.

In simpler terms:

“Take the result on the left side, and pass it as the first argument to the function on the right side.”

Traditional PHP (before 8.5)

    $result = strtoupper(trim(htmlspecialchars($input)));
    

This works fine, but it’s harder to read when functions get deeply nested.

 

PHP 8.5 With Pipe Operator

    $result = $input
    |> htmlspecialchars($$)
    |> trim($$)
    |> strtoupper($$);
    

 

Explanation:

  • $input is piped into htmlspecialchars()
  • Then the result is piped into trim()
  • Finally, that result is piped into strtoupper()

The $$ acts as a placeholder variable for the piped value.

Much cleaner and easier to read!

 

Basic Syntax

    $variable |> function_name($$);

  • Left side: The value or expression to be passed.

  • Right side: The function to receive that value.

  • $$: A special placeholder that represents the left-side result.

 

Detailed Example

Let’s take a simple example of processing user input before saving it:

Example Without Pipe Operator

$userInput = "   <b>Hello PHP!</b>   ";

$output = strtoupper(trim(strip_tags($userInput)));

echo $output; // HELLO PHP!
    

>Example Using Pipe Operator (|>)

    $userInput = "   <b>Hello PHP!</b>   ";

$output = $userInput
    |> strip_tags($$)
    |> trim($$)
    |> strtoupper($$);

echo $output; // HELLO PHP!

 

Benefits:

  • Readability improves significantly.
  • No deeply nested parentheses.
  • Easier to debug or comment out individual transformation steps.

Real-World Use Case

Use Case 1: Data Sanitization Pipeline

    $cleanData = $rawInput
    |> trim($$)
    |> stripslashes($$)
    |> htmlspecialchars($$)
    |> strtolower($$);

This pipeline clearly shows the step-by-step transformation of user input.

 

Use Case 2: Complex Array Transformations

   $numbers = [1, 2, 3, 4, 5];

$result = $numbers
    |> array_map(fn($n) => $n * 2, $$)
    |> array_filter($$, fn($n) => $n > 5)
    |> array_sum($$);

echo $result; // 18 (i.e., (6 + 8 + 10) = 24)

Pipe operators make it easy to process data sequentially — like a “pipeline” of transformations.

 

Use Case 3: Working with Objects

class Product {
    public function priceWithTax($price) {
        return $price * 1.18;
    }
}

$product = new Product();

$finalPrice = 100
    |> $product->priceWithTax($$)
    |> number_format($$, 2);

echo "₹" . $finalPrice; // ₹118.00

This approach makes the data flow explicit and readable.

 

Why It’s Important

Benefit Description
Improves Readability Removes nested parentheses and clarifies data flow
Encourages Functional Style Promotes clean and declarative programming
Simplifies Chaining Perfect for transforming values step-by-step
Easier Debugging Comment out or log any stage in the pipeline easily

 

Things to Remember

  • The $$ placeholder must appear on the right side.
  • It can’t replace multiple arguments — it’s for a single input value.
  • Works only with PHP 8.5 or later.
  • Not yet supported in older PHP versions (8.4 or below).

 

Experimental Example (Advanced)

Here’s how the pipe operator can even simplify custom pipeline functions:

   function double($x) { return $x * 2; }
function addFive($x) { return $x + 5; }
function square($x) { return $x * $x; }

$result = 10
    |> double($$)
    |> addFive($$)
    |> square($$);

echo $result; // ((10 * 2) + 5)^2 = 625

You can see the logical flow clearly — no nesting, no confusion.

 

Conclusion

The new Pipe Operator (|>) in PHP 8.5 is a huge leap toward cleaner, more readable, and functional programming.

It helps developers write elegant pipelines of data transformation without complex nesting.

If you often find yourself writing chained transformations, the pipe operator will make your PHP code more expressive and modern — aligning PHP with the syntax styles of other high-level languages.

 

Previous Article

PHP 8.5 in Development: Features, Release Date, and How It Beats PHP 8.4 (With Examples)

Next Article

PHP 8.5 New Array Functions: array_first() & array_last() Explained

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨