PHP Spread Operator Tutorial with Examples

The spread operator in PHP, introduced in PHP 5.6 and enhanced in PHP 7.4 and PHP 8.0, is a powerful feature that allows you to unpack values from arrays or traversable objects into function arguments or combine arrays.

It simplifies code by avoiding manual unpacking or merging operations.

In this tutorial, we will cover:

Let’s explore each of these use cases with examples and explanations.

1. Spread Operator in Function Arguments

The spread operator (…) can be used to pass an array of values as individual arguments to a function. This feature is especially useful when the function expects multiple arguments but you want to pass an array instead of explicitly providing each value.

Syntax:

function myFunction(...$params) {
    // Function code
}

Example (Using Spread Operator in Function Arguments):

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

$numbers = [1, 2, 3];

// Use the spread operator to pass the array as individual arguments
$result = sum(...$numbers);

echo $result;  // Outputs: 6
?>
  • In this example, the array $numbers is unpacked and passed as individual arguments to the sum() function.

Example (Multiple Arguments and Spread Operator):

<?php
function multiply($a, $b, $c, $d) {
    return $a * $b * $c * $d;
}

$values1 = [2, 3];
$values2 = [4, 5];

// Use multiple spread operators to pass multiple arrays
$result = multiply(...$values1, ...$values2);

echo $result;  // Outputs: 120
?>
  • Here, the spread operator is used with two arrays ($values1 and $values2), unpacking them into individual arguments for the multiply() function.

2. Spread Operator in Array Merging (PHP 7.4+)

Starting from PHP 7.4, the spread operator can also be used to merge arrays. This is a clean and efficient way to concatenate multiple arrays into one, replacing the need for functions like array_merge().

Syntax:

$array = [...$array1, ...$array2, ...$array3];

Example (Merging Arrays with Spread Operator):

<?php
$array1 = [1, 2, 3];
$array2 = [4, 5];
$array3 = [6, 7, 8];

// Use the spread operator to merge arrays
$mergedArray = [...$array1, ...$array2, ...$array3];

print_r($mergedArray);
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
)
  • The spread operator […$array1, …$array2, …$array3] merges all three arrays into a single array.

Example (Merging Arrays with Additional Elements):

<?php
$numbers = [1, 2, 3];
$letters = ['a', 'b', 'c'];

// Merge arrays and add extra elements
$combined = [...$numbers, 'x', 'y', ...$letters];

print_r($combined);
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => x
    [4] => y
    [5] => a
    [6] => b
    [7] => c
)
  • You can also include additional elements in the merged array when using the spread operator.

3. Spread Operator with Traversable Objects (PHP 8.0+)

In PHP 8.0, the spread operator was enhanced to support Traversable objects. This means you can now use it with objects that implement the Traversable interface (such as ArrayIterator from the SPL library).

Example (Using Spread Operator with Traversable Objects):

<?php
// Create a Traversable object (ArrayIterator)
$iterator = new ArrayIterator([1, 2, 3]);

$array = [...$iterator, 4, 5];

print_r($array);
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
  • In this example, the ArrayIterator object is traversed using the spread operator, and its values are unpacked into an array along with additional elements.

Comparison: Spread Operator vs. array_merge()

Before the spread operator was introduced, merging arrays was typically done using array_merge():

$array = array_merge($array1, $array2, $array3);

With the spread operator, the same task is simpler and cleaner:

$array = [...$array1, ...$array2, ...$array3];
  • The spread operator is more concise and flexible because you can easily insert individual elements into the merged array.

Summary of the PHP Spread Operator:

Use Case Description Example
Function Arguments Unpacks array values into individual function arguments myFunction(…$array);
Array Merging (PHP 7.4) Merges multiple arrays into one $array = […$arr1, …$arr2];
Traversable Objects (PHP 8.0) Unpacks values from traversable objects $array = […$iterator, …$arr];

Conclusion

The spread operator in PHP is a powerful tool for unpacking arrays or traversable objects and passing their elements to functions or merging arrays. Here’s what we covered in this tutorial:

  • Spread operator in function arguments for unpacking arrays and passing them to functions as individual arguments.
  • Spread operator in array merging (introduced in PHP 7.4), which allows you to merge arrays more concisely than array_merge().
  • Spread operator with traversable objects (introduced in PHP 8.0), extending the operator’s functionality to objects implementing the Traversable interface.

 

Related posts

PHP Static Methods: A Tutorial with Examples

PHP Deleting Files: A Tutorial with Examples

PHP Copying Files: A Tutorial with Examples