PHP variable length arguments?

让人想犯罪 __ 提交于 2019-11-30 20:35:44

Unlike Python's * operator or C#'s params keyword, in PHP you don't even have to specify the variable length arguments. As the second part starts off, "No special syntax is required."

As to the rest of the second paragraph: if you want to specify any required or unrelated arguments that come before the variable-length arguments, specify them in your function signature so your function can handle those. Then to get the variable-length arguments, remove the required variables from func_get_args(), like so:

function func($required) {
    // Contains all arguments that come after $required
    // as they were present at call time
    $args = array_slice(func_get_args(), 1);
}

You don't have to do this (you can still slice from func_get_args() and use its different elements accordingly), but it does make your code more self-documenting.

Here is a more realistic example:

function Average()
{
    $result = 0;
    $arguments = func_get_args();

    foreach ($arguments as $argument)
    {
        $result += $argument;
    }

    return ($result / max(1, func_num_args()));
}

Average(1, 2, 3, 4, 5); // 3

This is called a variadic function.

Since PHP 5.6, a variable argument list can be specified with the ... operator.

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)
#> }

As you can see, the ... operator collects the variable list of arguments in an array.

If you need to pass the variable arguments to another function, the ... can still help you.

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, the variable list of arguments can be forced to be all of the same type too.

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

There's no special syntax for variable length argument functions.

Simply use the func_num_args() and func_get_args() functions to get the arguments.

Example:

function callMe(){

    if(func_num_args() == 0){
        echo 'No arguments =)';
    }else{
        var_dump(func_get_args());
    }

}

The second portion is basically saying that once you start using the variable argument functions, then

function average() { ... }
function average(arg1, arg2, arg3) { ... }

work identically, just that the second version has 3 arguments explicity listed. That's all, don't try to read more into a man page than there is.

I usually do this to avoid kept changing function, and avoid error on argument order

function any_function($ops=array())
{
}

$ops = array('name'=>1, 'type'=>'simplexml', 'callback'=>...);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!