This website is currently under active development (Beta) 🚀. Some features are still work in progress.
Laravel Tutorial

Piper: Unlocking Laravel's Array and String Power in Pure PHP with the Pipe Operator

Admin User
Admin User
May 23, 2026
6 min read

Key Takeaways

  • Laravel's fluent collection and string manipulation methods are a joy to work with, offering concise and readable ways to transform data. What if you could brin...

Laravel's fluent collection and string manipulation methods are a joy to work with, offering concise and readable ways to transform data. What if you could bring that same expressive power to any PHP project, without pulling in the entire Laravel framework? Enter Piper, a brilliant package from Spatie that bridges this gap by porting these beloved Laravel-style helpers to standalone functions designed to work seamlessly with PHP 8.5's upcoming pipe operator (|>).

What is Piper? #

Piper is essentially a toolkit that extracts the best of Laravel's collection and string manipulation capabilities into a set of pure PHP functions. Its core idea revolves around providing familiar, powerful methods like map, filter, pluck for arrays, and slug, upper, studly for strings, but repackaged for broader use across any PHP application.

The key innovation lies in its design for the PHP 8.5 Pipe Operator. This operator significantly enhances readability and maintainability when chaining operations, transforming nested function calls or intermediate variable assignments into a linear, sequential flow.

Why Use Piper? #

Piper offers several compelling advantages:

  • Laravel-Style Fluency: Enjoy the same clear, expressive syntax for data transformations that Laravel developers love, but in any PHP project.
  • Enhanced Readability: The pipe operator, combined with Piper's functions, allows you to write complex data pipelines in a natural, left-to-right manner, making your code easier to understand at a glance.
  • Functional Programming Paradigm: Encourages a more functional style of programming, where data transformations are seen as a series of immutable operations.
  • Avoid Framework Overhead: Gain access to powerful helpers without the need to install the full Laravel framework, perfect for smaller scripts, microservices, or non-Laravel projects.
  • Cleaner Code: Say goodbye to deeply nested function calls or numerous temporary variables when performing multiple transformations.

Prerequisites #

To follow along and use Piper, you'll need:

  • PHP 8.5 or higher: Piper is specifically built for the pipe operator, which is slated for PHP 8.5.
  • Composer: For package management.

Note: As of writing, PHP 8.5 and the pipe operator are still in development/RFC phase. This tutorial assumes its final inclusion and stable syntax.

Installation #

Installing Piper is straightforward using Composer:

composer require spatie/piper

How to Use Piper: A Practical Guide #

Understanding the Pipe Operator (|>) #

Before diving into Piper's functions, let's grasp the concept of the pipe operator. It takes the result of the expression on its left and passes it as the first argument to the function or expression on its right. It uses $$ as a placeholder for the piped value within the right-hand expression.

Without Pipe Operator (Traditional Chaining/Nesting):

<?php

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

// Nested function calls $processedData = array_filter(array_map(fn($n) => $n * 2, $data), fn($n) => $n > 5);

// Or with intermediate variables $doubledData = array_map(fn($n) => $n * 2, $data); $processedData = array_filter($doubledData, fn($n) => $n > 5);

print_r($processedData); // Output: Array ( [2] => 6 [3] => 8 [4] => 10 )

With Pipe Operator:

<?php

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

$processedData = $data |> array_map(fn($n) => $n * 2, $$) |> array_filter($$, fn($n) => $n > 5);

print_r($processedData); // Output: Array ( [2] => 6 [3] => 8 [4] => 10 )

Notice how the pipe operator makes the flow much more linear and readable.

Using Piper's Array Helpers #

Piper provides standalone functions for common array transformations. Let's assume you've imported the necessary functions from the Spatie\Piper namespace (e.g., use function Spatie\Piper\{map, filter, unique, pluck};).

<?php

use function Spatie\Piper\{map, filter, unique, pluck};

$users = [
    ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'],
    ['id' => 2, 'name' => 'Bob', 'email' => '[email protected]'],
    ['id' => 3, 'name' => 'Alice', 'email' => '[email protected]'], // Duplicate name/email
    ['id' => 4, 'name' => 'Charlie', 'email' => '[email protected]'],
];

// Example 1: Get unique names, then uppercase them
$processedNames = $users
    |> pluck($$, 'name')
    |> unique($$)
    |> map($$, fn($name) => strtoupper($name));

print_r($processedNames);
// Output: Array ( [0] => ALICE [1] => BOB [3] => CHARLIE )

// Example 2: Filter users by ID > 2, then map to just their emails
$filteredEmails = $users
    |> filter($$, fn($user) => $user['id'] > 2)
    |> pluck($$, 'email');

print_r($filteredEmails);
// Output: Array ( [0] => [email protected] [1] => [email protected] )

Using Piper's String Helpers #

Piper also brings Laravel's powerful string manipulation functions. Let's assume importing `slug`, `upper`, `studly`.

<?php

use function Spatie\Piper\{slug, upper, studly};

$title = "This is a Great Blog Post Title!";

// Example 1: Create a URL-friendly slug
$postSlug = $title
    |> slug($$);

echo "Slug: " . $postSlug . "\n"; // Output: Slug: this-is-a-great-blog-post-title

// Example 2: Convert to StudlyCase
$studlyCase = "laravel_world_article"
    |> studly($$);

echo "StudlyCase: " . $studlyCase . "\n"; // Output: StudlyCase: LaravelWorldArticle

// Example 3: Chain multiple string operations (uppercase, then replace spaces with underscores)
$transformedString = "hello world from piper"
    |> upper($$)
    |> str_replace(' ', '_', $$); // Can mix with native PHP functions

echo "Transformed: " . $transformedString . "\n"; // Output: Transformed: HELLO_WORLD_FROM_PIPER

Piper vs. Laravel Collections #

You might wonder when to use Piper versus Laravel's built-in Collections. The choice largely depends on your project context:

  • Laravel Applications: If you're already within a full Laravel application, stick with Laravel's native Illuminate\Support\Collection and string helpers. They are tightly integrated and offer a more comprehensive feature set within the framework's ecosystem.
  • Plain PHP or Non-Laravel Projects: This is where Piper shines. For scripts, microservices, standalone packages, or projects not using Laravel, Piper provides a lightweight way to access similar powerful, readable data manipulation without framework dependencies.

Conclusion #

Piper is a fantastic addition to the PHP ecosystem, bringing the elegance and efficiency of Laravel's array and string helpers to a wider audience, beautifully integrated with the upcoming PHP 8.5 pipe operator. By enabling a more functional and readable coding style, Piper helps developers write cleaner, more maintainable code, regardless of their project's framework choice. Give it a try in your next PHP project that needs expressive data transformations!

FAQs

What is the PHP Pipe Operator (<pre><code>|></code></pre>)?
The PHP Pipe Operator (<pre><code>|></code></pre>) is a proposed feature for PHP 8.5 that allows you to pass the result of an expression on its left as the first argument to the function or expression on its right, using <pre><code>$$</code></pre> as a placeholder. It improves code readability for chained operations.
Do I need Laravel to use Piper?
No, that's the main point of Piper! It extracts Laravel-style array and string helpers into standalone functions, allowing you to use them in any plain PHP project without the overhead of the full Laravel framework.
What PHP version is required for Piper?
Piper is designed for PHP 8.5 or higher, specifically to leverage the new pipe operator (<pre><code>|></code></pre>). As of writing, PHP 8.5 is still in development, but Piper is built with this future feature in mind.
How does Piper compare to Laravel's native Collections?
For full Laravel applications, stick to Laravel's own <pre><code>Illuminate\Support\Collection</code></pre>. Piper is best suited for non-Laravel PHP projects or standalone scripts where you want Laravel-like data manipulation without the full framework dependency.

Want more content like this?

Explore more tutorials in the Laravel section.

Explore Laravel

You might also like