Kritim Yantra
May 30, 2025
|>
) in PHP 8.5?PHP 8.5 introduces a game-changing feature: the Pipe Operator (|>
). This operator allows you to chain functions together in a left-to-right flow, where the result of one function automatically becomes the input for the next.
Think of it like a pipeline: data flows through each function, getting transformed along the way.
β
No more nested calls.
β
No more temporary variables.
β
Just a clean, easy-to-read transformation chain.
Imagine you want to transform a string by applying several functions:
$result = trim(strtoupper(str_shuffle("Hello World")));
This works, but itβs hard to read β you have to start from the inside out.
Alternatively, you could use temporary variables:
$string = "Hello World";
$upper = strtoupper($string);
$shuffled = str_shuffle($upper);
$result = trim($shuffled);
But that feels a bit verbose.
Enter the Pipe Operator (|>
):
$result = "Hello World"
|> strtoupper(...)
|> str_shuffle(...)
|> trim(...);
π‘ Much cleaner, right?
The Pipe Operator takes the result of the left expression and automatically passes it as the first argument to the right callable.
β
The left side is an expression or value (like a string, array, object, or function result).
β
The right side must be a callable that accepts one required parameter.
$message = "OpenAI GPT"
|> strtoupper(...)
|> strrev(...)
|> trim(...);
echo $message;
// Output: "TPG IANEPO"
The Pipe Operator works with:
β
Built-in functions like strtoupper
, trim
, etc.
β
User-defined functions:
function addExclamation($text) {
return $text . "!";
}
$message = "Hello"
|> addExclamation(...);
β Static methods:
class TextUtils {
public static function greet($name) {
return "Hello, $name";
}
}
$name = "Alice"
|> TextUtils::greet(...);
β
Instance methods and objects with __invoke
.
β
Arrow functions and anonymous functions:
$name = "Bob"
|> fn($n) => strtoupper($n);
β
First-class callables using ...
.
π« The pipe operator is designed for single-argument functions only.
π« You canβt change the parameter position; the value is always passed as the first argument.
π« If a function does not accept any arguments, you cannot use it in a pipe chain.
π« Functions that return void
will pass null
to the next function.
β Single line:
$result = "OpenAI" |> strtoupper(...) |> strrev(...) |> trim(...);
β Multiline (more readable!):
$result = "OpenAI"
|> strtoupper(...)
|> strrev(...)
|> trim(...);
The Pipe Operator follows PHPβs existing type rules:
function sanitize($input) {
return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
}
$userInput = " <h1>Hello World!</h1> ";
$cleaned = $userInput
|> trim(...)
|> strtolower(...)
|> sanitize(...);
echo $cleaned;
// Output: <h1>hello world!</h1>
Feature | Details | |
---|---|---|
Operator | ` | >` |
Purpose | Chain single-argument functions (left-to-right) | |
Parameter requirement | One required parameter per function | |
Supported callables | Built-ins, user-defined, methods, closures, etc. | |
Type handling | Follows PHPβs type rules, strict_types respected | |
Best use case | Data transformation chains |
β
Makes your code cleaner and more readable.
β
Great for data transformation (strings, arrays, objects).
β
No more nested functions or temporary variables.
β
Brings PHP closer to functional programming concepts.
The Pipe Operator (|>
) in PHP 8.5 isnβt just syntactic sugar β itβs a powerful new tool for writing elegant, readable, and expressive code.
As you get comfortable with it, youβll find your PHP code looking cleaner and easier to maintain, especially when chaining multiple single-argument functions.
β¨ Start using it today, and make your PHP code flow like a river!
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 11, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google