Pipe Operator
The Pipe Operator |> is a language feature that enables functional
composition by chaining operations in a left-to-right syntax.
#How it works
Rather than nesting function calls or creating intermediate variables, the pipe operator passes the result of one expression directly as the first parameter to the next callable.
Traditional approach:
With pipe operator:
Both these examples produce identical results, but the pipe syntax transforms how we read and follow the flow of the code.
The traditional approach shows how calls are nested and therefore forces you to parse the code from the inside out.
You must first find bakeDonut(), then move outward for each topping.
The pipe operator follows a natural reading flow: start with the input, then follow each step sequentially. This eliminates the cognitive overhead of matching parentheses and makes it trivial to add, remove, or reorder the steps.
Practical Example
User processing pipeline:
$adminCount = getUsers() |> fn($users) => array_filter($users, isAdmin(...)) |> count(...);This pipeline:
- Retrieves all users from the system
- Filters the collection to admin users only
- Returns the total count of administrators
#Syntax
- Each callable must accept exactly one required parameter
- Data flows left-to-right through the pipeline:
A |> B |> C - Supports any valid PHP callable (functions, methods, closures, first-class callables)
First-Class Callable Notation
The pipe operator works well with PHP 8.1's
first-class callable syntax ...:
// First-class callable syntax$result = "hello world" |> strtoupper(...) |> trim(...);
// Works with any callable type$data = getUsers() |> fn($users) => array_filter($users, isAdmin(...)) // Function reference |> array_values(...) // Built-in function |> Processor::processUsers(...) // Static method |> $processor->transform(...); // Instance methodThe ... syntax creates a callable reference without invoking the function, making it useful for pipe chains.
Benefits
- Enhanced readability: Eliminates deeply nested function calls
- Improved maintainability: Linear data flow is easier to debug and modify
- Reduced complexity: Removes the need for temporary variables
- Performance optimization: Compiler-level optimizations
Partial Function Application
Work has begun on a complementary feature called
Partial Function Application targeting PHP 8.6.
This enhancement would introduce ? as a placeholder to the syntax and provide better control over parameter
placement in multi-argument functions.
Current limitation with pipe operator:
// Requires a closure for multi-parameter functions$result = $data |> fn($x) => str_replace(' ', '-', $x);Proposed syntax:
// Clean placeholder syntax with partial function application$result = $data |> str_replace(' ', '-', ?);The ? placeholder creates a partially applied function where the piped value fills the marked position.
This would remove the need to wrap steps in a closure for multi-parameter functions, making pipe chains more
readable and concise.
Also being considered:
- Multiple placeholders
- Named argument support
- Variadic placeholders for spread operations
While this feature is still being discussed, and not yet confirmed for any specific release, it does demonstrate the continued effort being made to improve PHP's programming capabilities.
#Further Reading
PHP RFC: Pipe Operator v3
PHP RFC: Partial Function Application (v2)
Pipe Operator in PHP 8.5
PHP 8.5: Pipe operator (|>)
First class callable syntax
Discovering PHP's first-class callable syntax
Discuss on Discord
Did you know this article uses Event Sourcing to display your donut and the code examples?
I made a video explaining it with football, if you want to know how it works.