PHP function with unlimited number of parameters

后端 未结 8 1713
执念已碎
执念已碎 2020-12-03 13:45

How do I write a function that can accept unlimited number of parameters?

What am trying to do is create a function within a class that wraps the following:

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 14:34

    If you are using PHP 5.6 or later version, argument lists may include the ... token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; Simply Using ... you can access unlimited variable arguments.

    for example:

    
    

    If you are using PHP version <= 5.5 then you can access unlimited parameterusing the func_num_args(), func_get_arg(), and func_get_args() functions.

    For example:

    = 2) {
            echo "Second argument is: " . func_get_arg(1) . "\n";
        }
        $arg_list = func_get_args();
        for ($i = 0; $i < $numargs; $i++) {
            echo "Argument $i is: " . $arg_list[$i] . "\n";
        }
    }
    
    foo(1, 2, 3);
    ?>
    

提交回复
热议问题