PHP get all arguments as array? PHP get all arguments as array? php php

PHP get all arguments as array?


func_get_args returns an array with all arguments of the current function.


If you use PHP 5.6+, you can now do this:

<?phpfunction sum(...$numbers) {    $acc = 0;    foreach ($numbers as $n) {        $acc += $n;    }    return $acc;}echo sum(1, 2, 3, 4);?>

source: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list


Or as of PHP 7.1 you are now able to use a type hint called iterable

function f(iterable $args) {    foreach ($args as $arg) {        // awesome stuff    }}

Also, it can be used instead of Traversable when you iterate using an interface. As well as it can be used as a generator that yields the parameters.

Documentation