Tsallaka zuwa abun ciki
Justice Tech School

Spread and Destructuring

The ... operator spreads arrays into calls and arrays. List destructuring with [] unpacks values and keys into variables.

Example
function sum(...$n) { return array_sum($n); }
$a = [1, 2, 3];
echo sum(...$a), "\n";
$m = [...$a, 4, 5];
echo implode(",", $m), "\n";
[$x, , $z] = $a;
["name" => $n, "age" => $age] = ["name" => "Ada", "age" => 21];
echo "$x $z $n $age\n";

Result

6
1,2,3,4,5
1 3 Ada 21

Exercise

Spread an array.

sum($a)

Quiz Test

What does [$a, $b] = $arr do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.