Coder Perfect

How to pass a PHP function a variable number of parameters

Problem

I have a PHP function that accepts a variable number of arguments (using func num args() and func get args()), however the amount of arguments I wish to provide it depends on the length of an array. Is it possible to use a PHP function that takes a variable number of arguments?

Asked by nohat

Solution #1

You might be interested in the call user func array function if your arguments are in an array.

If the number of arguments you want to pass depends on the length of an array, it probably means you can pack them into an array themselves — and use that one for the second parameter of call_user_func_array.

Your function will receive each element of the array you supply as a separate parameter.

For example, assuming you have the following feature:

function test() {
  var_dump(func_num_args());
  var_dump(func_get_args());
}

You can use an array to store your parameters, like this:

$params = array(
  10,
  'glop',
  'test',
);

Then, use the following syntax to invoke the function:

call_user_func_array('test', $params);

The following is the outcome of this code:

int 3

array
  0 => int 10
  1 => string 'glop' (length=4)
  2 => string 'test' (length=4)

ie, 3 parameters ; exactly like iof the function was called this way :

test(10, 'glop', 'test');

Answered by Pascal MARTIN

Solution #2

Using the… operator (also known as the splat operator in various languages), this is now available with PHP 5.6.x:

Example:

function addDateIntervalsToDateTime( DateTime $dt, DateInterval ...$intervals )
{
    foreach ( $intervals as $interval ) {
        $dt->add( $interval );
    }
    return $dt;
}

addDateIntervaslToDateTime( new DateTime, new DateInterval( 'P1D' ), 
        new DateInterval( 'P4D' ), new DateInterval( 'P10D' ) );

Answered by jonathancardoso

Solution #3

Instead of using func get args, you can use the… operator in Php 5.6. ().

As a result, you can receive all the parameters you pass using this:

function manyVars(...$params) {
   var_dump($params);
}

Answered by Salvador Dali

Solution #4

The… operator has been used to specify a variable argument list since PHP 5.6.

function do_something($first, ...$all_the_others)
{
    var_dump($first);
    var_dump($all_the_others);
}

do_something('this goes in first', 2, 3, 4, 5);

#> string(18) "this goes in first"
#>
#> array(4) {
#>   [0]=>
#>   int(2)
#>   [1]=>
#>   int(3)
#>   [2]=>
#>   int(4)
#>   [3]=>
#>   int(5)
#> }

The… operator creates an array from the variable list of inputs.

The… can still be useful if you need to send variable arguments to another function.

function do_something($first, ...$all_the_others)
{
    do_something_else($first, ...$all_the_others);
    // Which is translated to:
    // do_something_else('this goes in first', 2, 3, 4, 5);
}

Since PHP 7, all parameters in a variable list can be compelled to be of the same type.

function do_something($first, int ...$all_the_others) { /**/ }

Answered by Daniele Orlando

Solution #5

If you’re looking for a way to achieve this with $object->method, here’s what you can do:

call_user_func_array(array($object, 'method_name'), $array);

In a create function that calls a variable method name with variable parameters, I was successful.

Answered by danilo

Post is based on https://stackoverflow.com/questions/1422652/how-to-pass-variable-number-of-arguments-to-a-php-function