In php how would you create a function that could take an unlimited number of parameters: myFunc($p1, $p2, $p3, $p4, $p5...);
My next question is: how would you pass them into another function something like
function myFunc($params){
anotherFunc($params);
}
but the anotherFunc
would receive them as if it was called using anotherFunc($p1, $p2, $p3, $p4, $p5...)
We pass arguments in a function, we can pass no arguments at all, single arguments or multiple arguments to a function and can call the function multiple times.
Answer 1: When it comes to passing arguments to function, the maximum number of arguments that is possible to pass is 253 for a single function of the C++ programming language.
Which of the following PHP functions accepts any number of parameters? Explanation: func_get_args() returns an array of arguments provided. One can use func_get_args() inside the function to parse any number of passed parameters.
PHP Parameterized functions They are declared inside the brackets, after the function name. A parameter is a value you pass to a function or strategy. It can be a few value put away in a variable, or a literal value you pass on the fly. They are moreover known as arguments.
call_user_func_array('anotherFunc', func_get_args());
func_get_args
returns an array containing all arguments passed to the function it was called from, and call_user_func_array
calls a given function, passing it an array of arguments.
Previously you should have used func_get_args()
, but in a new php 5.6 (currently in beta), you can use ... operator
instead of using .
So for example you can write :
function myFunc(...$el){
var_dump($el);
}
and $el
will have all the elements you will pass.
Is there a reason why you couldn't use 1 function argument and pass all the info through as an array?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With