Skip to content
Advertisement

PHP Spread Syntax in Array Declaration

PHP supports the spread syntax for variadic functions.

In JavaScript, you can use the spread syntax to do this:

JavaScript

However, trying to do this in PHP:

JavaScript

Results in this error:

Parse error: syntax error, unexpected ‘…’ (T_ELLIPSIS), expecting ‘]’

Is using the spread syntax this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the same effect?

Advertisement

Answer

The spread operator in the arrays RFC has been implemented in PHP 7.4:

JavaScript

Caveat: The unpacked array/Traversable can only have integer keys. For string keys array_merge() is still required.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement