โž• PHP Operators
Estimated reading: 3 minutes 25 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“ค PHP Spread Operator

Or Copy Link

CONTENTS
Scroll to Top