PHP variable length arguments?

In Python and others, there is a special syntax for variable-length argument lists:

def do_something(*args): # do something do_something(1, 2, 3, 4, 5, ...) # arbitrarily long list 

I read the PHP manual and he said the following:

PHP 4 and above support variable arguments of variable length to user-defined. It is really quite simple using the func_num_args (), func_get_arg () and func_get_args () functions.

No special syntax is required, and the argument lists can be explicit with function definitions and will behave as usual.

I get the first part. You can pass as many arguments as you want for a function that takes no arguments and then gets them as an array using func_get_args() , etc. I really don't understand what the second part says.

So my question is: is there any special syntax for variable-length arguments, or some best practice that I don't know about? The approach that the guide offers seems to be kludgey at best and makes your function look like it takes no arguments (if I'm not mistaken). Should I not try to use this language function at all?

+7
source share
6 answers

Unlike the Python * operator or the C # params , in PHP you don’t even need to specify arguments of variable length. When the second part begins, "special syntax is not required."

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

 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 cut with func_get_args() and use its various elements accordingly), but it makes your code more self-documenting.

+10
source

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 the variational function .

+18
source

Since PHP 5.6 , a list of variables can be specified using 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 a list of variable arguments in an array.

If you need to pass variable arguments to another function, ... might 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 , a list of variable arguments can be forced to all of the same type.

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

There is no special syntax for variable length argument functions.

Just 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()); } } 
+2
source

The second part basically says that as soon as you start using variable function arguments, then

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

works identically, just that the second version contains 3 arguments. That’s it, don’t try to read more on the manual page than it is.

+1
source

I usually do this to avoid changing the function and to avoid errors in the order of the arguments

 function any_function($ops=array()) { } $ops = array('name'=>1, 'type'=>'simplexml', 'callback'=>...); 
0
source

All Articles