Only arrays and Traversables can be unpacked¶
Description¶
The triple dot ... operator works on arrays and traversables, such as generator, iterators… It cannot work on other data structures.
Also, ... has a lower priority than the null-coalesce operator ??, so it applies to both branch of the operator.
Example¶
<?php
$a = null;
foo(...$a);
foo(...$a ?? range(1,2));
// displays 1, 2
function foo() {
print_r(func_get_args());
}
?>
Solutions¶
Check that the value is an array or a traverable before using the
...operator.Use the
??operator to give a value tonullbefore the...operator.Use the
?:operator to give a value to empty data before the...operator.