I have a PHP function that takes a variable number of arguments (using func_num_args() and func_get_args()), but the number of arguments I want to pass the function depends on the length of an array. Is there a way to call a PHP function with a variable number of arguments?
I have a PHP function that takes a variable number of arguments (using func_num_args()
Share
If you have your arguments in an array, you might be interested by the
call_user_func_arrayfunction.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.Elements of that array you pass will then be received by your function as distinct parameters.
For instance, if you have this function :
You can pack your parameters into an array, like this :
And, then, call the function :
This code will the output :
ie, 3 parameters ; exactly like iof the function was called this way :