PHP Spread Operator (with Advanced Program Examples)

What is a PHP spread operator?

The PHP spread operator is also known as the splat operator or scatter operator which comes from the concept of unpacking arguments.

This concept proposes to apply the function of unpacking arguments to array expressions.

To implement the PHP spread operator use the three dots as prefixes to the name of an array.

It then spreads the array elements of that argument, but the spread operator will only expand Traversable arrays and objects.

Example Program using PHP Spread Operator

Sample Code:

<?php

$array1 = [11, 22, 33];
$array2 = [1, 2, 3, ...$array1];

echo "<pre>";
print_r ($array2);
echo "</pre>";
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 11
    [4] => 22
    [5] => 33
)

Code Explanation:

The first example informs the program to unpack the argument the $array1 and merge them with the values of $array2.

Take note: The <pre></pre> tag is applied to show a readable output for the readers.

Example Program using PHP Spread Operator Multiple Times

Sample Code:

<?php
$ones = [1, 2, 3];
$twos = [4, 6, 8];
$both = [...$ones, ...$twos];
echo "<pre>";
print_r($both);
echo "</pre>";
?>

Output:

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

Code Explanation:

In this example, we have the three defined arguments and each of them has its own set of arrays.

The first argument is the $ones with [1, 2, 3] as its value set.

Next is the $twos argument with the value of [4, 6, 8], and we will merge the $ones and $twos in the third argument.

To do this, we use the three dots prefix to unpack the arrays in $ones and $twos as the value of the $both argument.

Example Program using PHP Spread Operator with a Return Value of a Function Call

Sample Code:

<?php

function get_random_numbers()
{
    for ($a = 0; $a < 5; $a++) {
        $x[] = rand(1, 100);
    }
    return $x;
}

$x = [...get_random_numbers()];

echo "<pre>";
print_r($x);
echo "</pre>";
?>

Output:

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

Code Explanation:

In this example, the function that we use is the get_random_numbers().

Inside this function, we define the base variable $a within the for statements and variable $x containing the random array that will serve as output.

To execute the program, we call the function get_random_numbers and place it as the array value of the $x.

Then, upon running the program, your console will display the random numbers as the array output as in the example.

Example Program using PHP Spread Operator with a Generator

Sample Code:

<?php

function even_number()
{
    for($a = 2; $a < 20; $a += 4){
        yield $a;
    }
}

$num = [...even_number()];

echo "<pre>";
print_r($num);
echo "</pre>";
?>

Output:

Array
(
    [0] => 2
    [1] => 6
    [2] => 10
    [3] => 14
    [4] => 18
)

Code Explanation:

The sample code tries to show you a possible process when we can automatically generate numbers out of the given values.

So we have an anonymous function even_numbers and we created a concept of generating numbers by 4 from 2 without exceeding it to 20.

Next, we will try the generator by calling the function as an array value of the argument $num.

Then we use the print_r() function to display the generated numbers (by 4) from 2 to 20.

Example Program Using PHP Spread Operator with a Traversable Object

Sample Code:

<?php

class color implements IteratorAggregate
{
    private $COLOR = ['black','white','grey'];

    public function getIterator()
    {
        return new ArrayIterator($this->COLOR);
    }
}

$sample = new color();
$COLOR = [...$sample];

echo "<pre>";
print_r($COLOR);
echo "</pre>";
?>

Output:

Array
(
    [0] => black
    [1] => white
    [2] => grey
)

Code Explanation:

This example shows that you can use PHP’s spread operator on any object that implements the Traversable interface, not just an array. 

Example Program Spread Operator and Named Arguments

Sample Code:

<?php
function sample_format (string $first, string $middle, string $last): string
{
    return $middle ?
        "$first $middle $last" :
        "$first $last";
}

echo sample_format($first = 'Jay', $middle = 'L.', $last = 'Steel');
?>

Output:

Jay L. Steel

Conclusion

In conclusion, the discussion about the PHP spread operator is now covered and completed the necessary explanation in different areas of the topic.

It has its corresponding details in each example program which I believe brightens your understanding of the spread operator.

Additionally, the example above is usable for free! Try those now and create a project out of it to improve your abilities in the field of PHP language.

Leave a Comment