Three Dots¶
Three dots, is an operator with several usages and names.
variadic, to make the last parameter collect all arguments in an array
spread, to expand elements of an array into arguments positions
spread in array, to merge several arrays in one
closure creator, when used standalone in a methodcall.
The various features of this operator were introduced in different PHP versions.
<?php
$a = [2, 3];
// equivalent to [1, 2, 3], or array_merge([1], $a);
$array = [1, ...$a];
// spread operator, in function call
// same as foo(0, 1, 2, 3, 4);
foo(0, ...$array, 4);
// variadic argument (the last one)
function foo($special, ...$others) {
// with the above call
// $special === 0
// $others === [1, 2, 3, 4]
}
// PHP 8.1
$closure = strlen(...);
echo $closure('Ab'); // display 2, by calling strlen
?>
Related : Variable Arguments, Array Spread, Ellipsis, First Class Callable
Added in PHP 5.6