The first appearance for PHP spread operator was in PHP7.4, it used to unpack data from function arguments, and arrays.

The expression of PHP spread operator is written as three dots ... which allows to extract the data from the arrays or function arguments.

So the basic syntax for the spread operator as the below.

...$variable
...array()

In other words, it places the extracted values of an array, function argument instead of the three dots which its new position. For example.

<?php 
   $array1 = array(10, 20, 30);
   $array2 = array(60, 70, 80);
   $main   = array( ...$array1, 40, 50, ...$array2); 
   print_r( $main );
?>

The output

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
    [7] => 80
)

Anyway, Let’s understand how it does work.

How the PHP Spread Operator Works

Basically, the three dots are the new place for the extracted values, so when you put them together beside the array or the variable. It extracts the variable or the array values and put them instead of these 3 dots.

Anyway, the following figure shows you how it works.

PHP Spread Operator ( Unpack Array )

The PHP spread operator can also work with a function argument to give us unlimited arguments when call it in another place. Let’s see how it works in the following section. Let’s see examples.

Unpack an Array with PHP Spread Operator

The following example contains 4 arrays.

<?php
   $names = array("Ahmed","Peter");
   $tasks = array(1, 2, 10, ...$names, 105, 100); 
   $lists = array( ...$names, ...$tasks);
   $print = array(...$lists, "PHP Tutorials");
   
   echo "</pre>";
   print_r($print);
   echo "</pre>";
?>

How it works:

  • There is no spread operator in the $name array.
  • In the $tasks array it will unpack $name array and takes the values of it. So the result would be like this: [1, 2, 10, "Ahmed", "Peter", 105, 100].
  • The $lists array will unpack the $names array and the $tasks array. But here we have duplicated values for the $names array because we unpacked it in the previous stage, so the result of the $list array would be like this: ["Ahmed","Peter", 1, 2, 10, "Ahmed","Peter", 105, 100].
  • The last phase will print this output: ["Ahmed","Peter", 1, 2, 10, "Ahmed","Peter", 105, 100, "PHP Tutorials"].

But how to ignore the repeated values with duplicated indexes? To do that, you have to use the addition operator (+) to collect the arrays together, for example.

<?php
  $array1 = array( 'a' => "Ahmed", 'c' => 'Kamilia' );
  $array2 = array( 'c' => 'Hassan', 'b' => 'Peter' );
  $main = $array1 + $array2;
 
echo "<pre>";
 print_r($main);
echo "</pre>";
?>

This will print the below result.

Array
(
    [a] => Ahmed
    [c] => Kamilia
    [b] => Peter
)

With the same example, I will use the spread operator [ ...$array1, ...$array2 ]. This will print the overwritten values.

Array
(
    [a] => Ahmed
    [c] => Hassan
    [b] => Peter
)

Another example for passing an array variable by reference.

Actually, you could not able to use the PHP spread operator with the reference operator. You can only do that by a variable as a broker.

<?php
   $array1 = [1, 2, 3];
   $main = array(...&$array1, 4, 5, 6);
?>

This will show you the following error.

Parse error: syntax error, unexpected token "&" in index.php

To fix that, you can pass the reference value into a variable as a broker, like the below example.

<?php
   $array1 = [1, 2, 3];
   $refs   = &$array1; 
   $main = array(...$refs, 4, 5, 6);
?>

The output:

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

Passing a function callback into array within a spread operator.

<?php
   function cfunc() {
      $value = array(100, 200, 300);
      return $value;
   }
   $array = [...cfunc(), 10, 20];
   print_r( $array );
?>

The output:

Array
(
  [0] => 100
  [1] => 200
  [2] => 300
  [3] => 10
  [4] => 20
)

Also, it can work with named function arguments.

To learn more about the function named arguments read this article.

<?php
    function extra(string $name, int $age, string $job ): string {
     return sprintf( "My name is %s and I have %d, working as a web developer at CodedTag company.", $name, $age, $job );
   }
   
   
   
   $array = array(
    "job" => "Web Developer",
    "age" => 34,
    "name" => "Montasser" 
  );

   echo extra( ...$array );
?>

Using the Spread Operator as a Function Parameter

You can specify only one parameter in the function with the PHP spread operator to allow PHP callback passing unlimited arguments. Let’s see that by example.

<?php 
  function my_func($value1, ...$value2) {
    // Accessing the function 
  }
?>

When doing this, you tell the PHP interpreter that you can pass unlimited arguments when you call this function in another place as a callback.

<?php
   my_func( "value 1 ", "value 2", "value 3", "value 4", ..... );
?>

But the question is: how to access these unlimited arguments within the function?

Actually, the PHP spread operator is also packing the values in an array, so it considers that passed data are two values the first one is $value1 and the second is an array. So when print the second parameter, it will show you an array. For example.

<?php
   function printing_args( $name, ...$val ) {
      echo $name;
      echo "<pre>";
      print_r($val);
      echo "</pre>";
   }

   // Calling the function
   printing_args( 
            "CodedTag.com", // => an required argument 
            "Learn to code for free",  // => // optional
            "Web Developer",  // optional
            "PHP tutorials", // optional
            "Developing" // optional
   );
?>

The output:

CodedTag.com
Array
(
    [0] => Learn to code for free
    [1] => Web Developer
    [2] => PHP tutorials
    [3] => Developing
)

If the second argument has no value, it will print an empty array. This doing the same task as the PHP built-in function “func_get_args()“.

Unpacking an Associative Array

As you know, the PHP associative array is an array has key names instead of the numbers. And here you can also use the spread operator, but that will work in PHP8.1. For example.

<?php
   $arr = array(
      "name"    => "Montasser",
      "job"     => "Senior Full-Stack Developer",
      "company" => "CodedTag.com"
   );

   $basic = array( "PHP", false, "C#", ...$arr, "C++", "Seo", 1, 2, true );
   print_r( $basic );
?>

The following output was in PHP8.1:

Array
(
    [0] => PHP
    [1] => 
    [2] => C#
    [name] => Montasser
    [job] => Senior Full-Stack Developer
    [company] => CodedTag.com
    [3] => C++
    [4] => Seo
    [5] => 1
    [6] => 2
    [7] => 1
)

Otherwise, if you are using a PHP version 8 or any old version, it will show you the following error.

Fatal error: Uncaught Error: Cannot unpack array with string keys in /index.php:8 

PHP Spread Operator with Type Hinting

To learn more about type hinting you have to read this article. To use the PHP spread operator with type hint, see the below example.

<?php
  class Car {
     public $speed = 120;
  }
  
  function expose( Car ...$var ) {
    var_dump( $var );
  }
 

  expose( new Car());
?>

The output:

array(1) { [0]=> object(Car)#1 (1) { ["speed"]=> int(120) } }

If we passed another data type such as this array : [1,2,3,4,5]. It will show you the below error.

Fatal error: Uncaught TypeError: Argument 1 passed to expose() must be an instance of Car, array given, called in index.php

Wrapping Up

  • The PHP spread operator used to change the three dots with the array values.
  • Prior to PHP8.1, you were not able to use the associative array with the spread operator.
  • You can also use the named arguments for the function with spread operator.