Spread Operator (...)
Also Known As
splat operator
... operator PHP
argument unpacking
TL;DR
Unpacks arrays or traversables into function argument lists or array literals; also used for variadic parameters.
Explanation
PHP's spread operator (...) serves two purposes: in function signatures it collects remaining arguments into an array (variadic), and in call sites or array literals it unpacks an iterable into individual elements. Since PHP 8.1, string-keyed arrays can be spread in function calls (named arguments). This enables array merging ($merged = [...$a, ...$b]) as a readable alternative to array_merge(), and passing stored argument lists to functions.
Common Misconception
✗ The spread operator can only be used with indexed arrays. Since PHP 8.1 the spread operator supports string-keyed arrays in function calls and array expressions — earlier versions throw a fatal error, so check the PHP version before relying on this.
Why It Matters
The spread operator (...) unpacks arrays into function arguments or array literals — eliminating call_user_func_array() boilerplate and enabling clean array merging with override semantics.
Common Mistakes
- Spreading a non-array or non-Traversable — causes a TypeError at runtime.
- Using spread for array merging when array_merge() semantics (re-indexing numerics, last-write-wins for strings) are needed.
- Spreading associative arrays in PHP 7 — string key spread was only added in PHP 8.1.
- Not knowing spread works in array literals: $merged = [...$defaults, ...$overrides].
Code Examples
✗ Vulnerable
// Old verbose approach:
$args = [1, 2, 3];
call_user_func_array('array_merge', $arrays); // Replaced by spread
// With spread operator:
function sum(int ...$nums): int { return array_sum($nums); }
$numbers = [1, 2, 3];
echo sum(...$numbers); // Clean argument unpacking
✓ Fixed
// Spread in function calls
function add(int $a, int $b, int $c): int { return $a + $b + $c; }
$args = [1, 2, 3];
echo add(...$args); // 6
// Spread in array literals
$first = [1, 2, 3];
$second = [4, 5, 6];
$merged = [...$first, ...$second]; // [1,2,3,4,5,6]
// PHP 8.1 — spread with string keys (named arguments style)
$defaults = ['color' => 'blue', 'size' => 'M'];
$options = ['size' => 'XL', ...$defaults]; // ['size' => 'M', 'color' => 'blue']
// Note: later keys win — order matters
// Variadic parameters
function sum(int ...$nums): int { return array_sum($nums); }
sum(1, 2, 3); // 6
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
28
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 8
Perplexity 3
SEMrush 3
Ahrefs 2
Google 1
ChatGPT 1
Bing 1
Also referenced
How they use it
crawler 19
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Use the spread operator (...) to unpack arrays as function arguments and merge arrays — it replaced call_user_func_array() and is cleaner than array_merge() for combining indexed arrays
📦 Applies To
PHP 5.6+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
call_user_func_array() still used PHP 5.6+; array_merge() for indexed arrays when spread would be cleaner; no ... spread on function calls with arrays
Auto-detectable:
✓ Yes
rector
phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✓ Auto-fixable
Fix: Low
Context: Function