logo
podcast Podcast
get help Get Unstuck

What do these three dots mean?

Keeping up with newer PHP syntax

Joel Clermont
Joel Clermont
2024-08-15

Today I wanted to talk about what it means when you see three dots ... in PHP. This is not an easy thing to search for either. What do you type into the search box "Hey what are these three dots in PHP"?

The oldest usage I'm aware of was back in PHP 5.6, when you could use it to specify variadic functions, which is a fancy way of saying functions that can accept a variable number of arguments.

Then in PHP 7.4, the same three dot syntax was used as a general purpose spread operator for any array, not just limited to variable argument lists passed to functions. I use this one a lot, and can't imagine PHP without it today.

But more recently, in PHP 8.1, a new usage of ... was introduced as a new syntax for creating a callable.

Let's dig into this a little bit, since I think it's the one that might be the most confusing at first glance.

It is useful to put the old and new syntax side by side:

// old syntax
array_map($array, 'is_numeric');

// new syntax
array_map($array, is_numeric(...));

Instead of specifying the callable as a string, you can now use the ... syntax to refer to the callable directly.

These two functions do basically the same basic thing, but the newer syntax doesn't require any special work for static analysis tools to understand it.

Also, this callable syntax can be used with other types of functions that can't be represented as strings, like any of the string helpers:

array_map($array, \Str::lower(...));

Another subtle, but important difference is that the new syntax uses the scope where the callable is defined. So let's say you were returning a callable from a class, and it referred to a private class method. The old syntax would fail, but the new syntax would work just fine because the private method was in scope when the callable was created.

Here to help,

Joel

P.S. Is your project stuck on an older version of PHP or Laravel? We can help get you updated.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.