← All writing

PHP 8.5 pipe operator: clean a supplier code without nesting functions

PHP 8.5+Sources checked 2026-09-12

Input string → remove edge whitespace → uppercase → AX-204

A spare-parts import receives the code " ax-204 ". The database expects "AX-204". This small example is a good place to understand the pipe operator before putting it into a much larger import.

Build one readable transformation

Save this as pipe.php and run it with PHP 8.5 or newer. The version matters: an older PHP parser cannot understand this syntax.

<?php
$incoming = "  ax-204  ";
$code = $incoming
    |> trim(...)
    |> strtoupper(...);
assert($code === 'AX-204');
echo $code, PHP_EOL;

Each stage receives the preceding value. The three dots create a callable; they do not execute the function immediately. Expected output: AX-204. This is a transformation of a known string, not input validation.

Add a stage with another argument

A function needing more than one argument needs an adapter. Parentheses around an arrow function are required here.

<?php
$label = "  ax-204  "
    |> trim(...)
    |> (fn (string $value) => str_replace('-', '/', $value))
    |> strtoupper(...);
assert($label === 'AX/204');
echo $label, PHP_EOL;

In this example the supplier changes separators, so the adapter owns that specific rule. Keeping it visible prevents an unexplained replacement from hiding inside a general-purpose helper.

Know where to stop

I would keep database writes, network requests, and error recovery outside this particular chain. If the supplier sends an array, validate the payload and reject it before these string operations. If codes are case-sensitive, remove the uppercase stage entirely. A convenient language feature cannot decide the supplier's identifier contract.

Run the example with an empty string, a code containing two hyphens, and a value already normalized. Decide separately whether each result is acceptable. Passing the transformation test does not mean an empty code may be inserted into the catalog.

The operator is already part of PHP 8.5; it is not an upcoming PHP 8.6 feature. PHP's functional operator reference documents callable requirements and arrow-function parentheses.