CodeNewbie Community 🌱

Montasser Mossallem
Montasser Mossallem

Posted on

Spread Operator in PHP: What It Is and How to Use It

In this guide, you’ll learn what the spread operator is, how it works, where it fits best, and how it compares to older approaches.

What Is the PHP Spread Operator?

The PHP spread operator (...) lets you extract arrays into individual elements.

You can use it to merge arrays, pass values into functions, or build new arrays from existing ones.

The basic syntax uses three dots before an array or variable:

$numbers = [1, 2, 3];
$newArray = [...$numbers, 4, 5];
Enter fullscreen mode Exit fullscreen mode

This expands $numbers into [1, 2, 3, 4, 5].

function sum($a, $b, $c) {
    return $a + $b + $c;
}

$args = [1, 2, 3];
echo sum(...$args); // 6
Enter fullscreen mode Exit fullscreen mode

Here are versions that support the spread operator in PHP

  • PHP 5.6+: Spread in function arguments (only arrays, as variadics).
  • PHP 7.4: Spread in arrays ([...]).
  • PHP 8.1: Spread with objects that implement Traversable.

Using the Spread Operator with Arrays in PHP

You can use ... inside arrays to insert elements from other arrays:

$a = [1, 2];
$b = [3, 4];
$merged = [...$a, ...$b]; // [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Merging Arrays with the Spread Operator

The spread operator offers a clean way to merge indexed arrays:

$a = ['a', 'b'];
$b = ['c', 'd'];
$result = [...$a, ...$b]; // ['a', 'b', 'c', 'd']
Enter fullscreen mode Exit fullscreen mode

It only works with arrays having integer keys. For associative arrays, keys may get overwritten or ignored.

Unpacking Arrays into Function Arguments

You can pass array elements as separate function arguments using ...:

function greet($name, $age) {
    echo "Hello $name, age $age.";
}

$args = ['John', 30];
greet(...$args);
Enter fullscreen mode Exit fullscreen mode

Using the Spread Operator with Objects (PHP 8.1)

From PHP 8.1, you can unpack Traversable objects into arrays:

class Data implements IteratorAggregate {
    public function getIterator() {
        return new ArrayIterator([1, 2, 3]);
    }
}

$data = new Data();
$array = [...$data]; // [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

The class must implement Traversable.

Unpacking Traversable Objects in Arrays

The spread operator only works with iterable objects (implementing Traversable). It does not work with regular objects.

$obj = (object)['a' => 1, 'b' => 2];
// [...$obj] will throw an error
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

The spread operator makes PHP code cleaner and more readable. You can use it to merge arrays, pass function arguments, or unpack values from iterable objects. It works best with indexed arrays and simplifies many tasks that used to require array_merge() or verbose loops.

Starting with PHP 7.4 for arrays and PHP 8.1 for objects, the spread operator fits well into modern PHP workflows. Just remember its limits—avoid using it with associative arrays or non-Traversable objects.

Use it when you need clear, simple array manipulation. Skip it when working with complex keys or deep merges.

Go to flatcoding.com to see more tutorials.

Top comments (0)