Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

The many uses of "..." ellipsis operator in PHP

With the evolution of PHP to its modern version, the one operator that has been used thoroughly across various places is the ellipsis operator (...).

I see there’s been some confusion around the PHP community about the use of this operator. And so, in this article, I’m going to discuss the various use cases of this operator in PHP.

Variadic functions and methods

Variadic functions are functions that accept a variable number of arguments. The ellipsis operator here can be used to define a variadic function in PHP.

The ellipsis operator is also called the “rest” operator in this context.

For instance, the following is a variadic function.

function sum(...$numbers)
{
    return array_sum($numbers);
}

echo sum(1, 2, 3, 4, 5); // 15

As you can tell, the sum function accepts a variable number of arguments and returns the sum of all the arguments since the $numbers parameter is an array of all the arguments passed to the function.

Similarly, Variadic methods are methods that accept a variable number of arguments. The ellipsis operator can be used here to define a variadic method in PHP.

For instance, the following is a variadic method.

class Calculator
{
    public function sum(...$numbers)
    {
        return array_sum($numbers);
    }
}

$calculator = new Calculator;

echo $calculator->sum(1, 2, 3, 4, 5); // 15

Array unpacking

Array unpacking is a way to pass an array of values as arguments to a function or a method using the ellipsis operator.

The ellipsis operator is also called the “spread” operator in this context.

For instance, the following function accepts three arguments.

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

So, if you want to pass an array of values to this function, you can do so using the ellipsis operator like so.

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

echo sum(...$numbers); // 6

As you can tell, the sum function accepts three arguments. But, we’re passing an array of five values to it.

So, in this case, the first three values of the array are passed as arguments to the function, and the rest of the values are ignored.

Variadic array merging

Variadic array merging is a way to merge multiple arrays into a single array. And the ellipsis operator can be used to merge multiple arrays into a single array using array unpacking.

For instance, the following code merges two arrays into a single array.

$numbers = [1, 2, 3];
$alphabets = ['a', 'b', 'c'];

$merged = [...$numbers, ...$alphabets];

var_dump($merged);
// [1, 2, 3, 'a', 'b', 'c']

From PHP 8.1 onwards, you can also merge associative arrays using the ellipsis operator.

First-class callables

Lastly, from PHP 8.1 onwards, the ellipsis operator can be used to pass in a callable as a Closure to a function or a method using something called “first-class callables”.

Here’s an example.

function cube($n)
{
    return ($n * $n * $n);
}

$a = [1, 2, 3, 4, 5];
$b = array_map(cube(...), $a);

print_r($b);
// [ 1, 8, 27, 64, 125 ]

As you can tell, the array_map function uses the cube function as a callback to map over the $a array. But the trick here is, we’re passing the cube function as a callable using the ellipsis operator.

This means the cube function is not called directly. Instead, it’s passed as a callable to the array_map function which then calls it internally.

The advantage of this syntax is that the cube function is accessible to static analysis, and respects the scope at the point where the callable is created. On top of it, it’s now IDE friendly as well since now you can jump over to the functions directly in supported IDEs!

The equivalent code without the ellipsis operator would be,

$b = array_map('cube', $a);

As you can tell, we have to pass the cube function as a string to the array_map function. This means this is not IDE-friendly since the IDE doesn’t know where the cube function is defined.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?