Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Operators

The PHP spread operator (...)

Reference for the PHP spread operator ...: unpack arrays and Traversables into arguments or array literals, string keys since 8.1, and common mistakes.

The spread operator ... unpacks an array or a Traversable in place. In a function call it turns the elements into separate arguments. Inside an array literal it inserts the elements at that position. The same three dots in a parameter list do the opposite job and collect arguments into an array (a variadic parameter).

Spread is the modern replacement for call_user_func_array() and for many array_merge() calls. It is evaluated left to right, so later elements can overwrite earlier ones when keys collide.

PHP
sum(...$numbers);              // unpack into arguments
$all = [...$first, ...$second]; // unpack into a new array
function sum(int ...$n) {}      // collect (variadic parameter)

Try it

PHP Spreading into calls and arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP String keys, named arguments and generators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
f(...$iterable)
[...$iterable, ...]
function f(Type ...$name)
Name Description
$iterable An array or any Traversable (an iterator or a generator). Other values throw an Error.

Result: in a call, one argument per element; in an array literal, the elements are inserted in order.

Errors: unpacking a non-iterable value (for example null or an int) throws Error ("Only arrays and Traversables can be unpacked").

Usage notes

  • Integer keys are renumbered in array literals: [...[5 => "x"], ...[5 => "y"]] becomes a list with keys 0 and 1. Unlike +, spread never keeps the first value for a key.
  • String keys are kept and overwritten (PHP 8.1+): in [...$a, ...$b] the later string key wins, just like array_merge(). Before PHP 8.1 string keys in array spread were an error.
  • String keys in calls become named arguments (PHP 8.1+). An unknown name throws an Error, and a positional element after a named one is not allowed. See named arguments.
  • Generators and iterators work. The traversable is consumed once. In array literals the keys are ignored for integer keys.
  • Variadic parameters use the same token but collect arguments. A variadic parameter must be last and may be typed (int ...$n).
  • Not a deep merge. Nested arrays are copied as values; use array_merge_recursive() when you need recursion.
  • null is not iterable. Guard with ?? [] when a value may be missing.
  • Performance. Spread copies the elements into a new array, so avoid it in tight loops over large arrays.