➕ PHP Operators
Estimated reading: 3 minutes 394 views

PHP Spread Operator – Function & Array Expansion Guide

Introduction – Why PHP Spread Operator Matters

The spread operator (...) in PHP is a powerful feature introduced in PHP 5.6 and enhanced in later versions. It provides a concise and elegant way to unpack arrays or arguments into functions or merge arrays — reducing boilerplate code.

In this guide, you’ll learn:

  • What the spread operator is and how it works
  • How to use it in function arguments and arrays
  • Version-specific behaviors
  • Practical examples and limitations

What is the Spread Operator in PHP?

The spread operator ... is used to expand arrays or traversables into individual elements. It’s most commonly used for:

  • Unpacking arguments into a function call
  • Unpacking arrays into a new array (PHP 7.4+)

Syntax & Use Cases

1. Spread in Function Arguments (PHP 5.6+)

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

$values = [1, 2, 3];
echo sum(...$values);  // Output: 6

Explanation:
The spread operator unpacks the array elements and passes them as individual arguments to the function.


2. Spread in Function Definitions (Variadic Parameters – PHP 5.6+)

function greet(...$names) {
  foreach ($names as $name) {
    echo "Hello, $name\n";
  }
}

greet("Alice", "Bob", "Charlie");

Explanation:
Here, ...$names collects any number of arguments into an array.


3. Spread in Array Expressions (PHP 7.4+)

$arr1 = [1, 2];
$arr2 = [3, 4];

$merged = [...$arr1, ...$arr2];
print_r($merged);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Explanation:
You can use ... to unpack arrays directly inside another array. Keys are reindexed unless associative.


4. Spread with Associative Arrays (PHP 8.1+)

$a = ["x" => 1];
$b = ["y" => 2];
$combined = ["z" => 0, ...$a, ...$b];
print_r($combined);

Output:

Array
(
    [z] => 0
    [x] => 1
    [y] => 2
)

Explanation:
From PHP 8.1 onward, associative arrays are preserved when using spread syntax.


Version Compatibility

FeaturePHP Version
Spread in function calls5.6+
Variadic parameters (function definition)5.6+
Spread in numeric arrays7.4+
Spread in associative arrays8.1+

Limitations

  • Cannot spread null or non-array values
  • Associative keys are only preserved in PHP 8.1+
  • Cannot unpack strings or scalar types

Summary – Recap & Next Steps

The PHP spread operator helps you write shorter, cleaner, and more readable code, especially when working with dynamic arrays and function arguments.

Key Takeaways:

  • ... unpacks arrays into arguments or array items
  • Use in function calls (...$array) or variadic functions (function fn(...$args))
  • Spread in array context requires PHP 7.4+ (associative arrays in PHP 8.1+)

Real-World Relevance:
Used in argument forwarding, dynamic merging, variadic utilities, and API request building.


Frequently Asked Questions (FAQs)

What is the spread operator in PHP?
It expands arrays or arguments using the ... syntax.

Can I use the spread operator to merge arrays?
Yes. Use [...$array1, ...$array2] (requires PHP 7.4+).

Does it preserve keys in associative arrays?
Only in PHP 8.1 and above.

What’s the difference between ...$array in call vs definition?
In a function call, it unpacks; in a function definition, it collects arguments.


Share Now :
Share

📤 PHP Spread Operator

Or Copy Link

CONTENTS
Scroll to Top