I want to create a function (my_function()
) getting unlimited number of arguments and passing it into another function (call_another_function()
).
function my_function() { another_function($arg1, $arg2, $arg3 ... $argN); }
So, want to call my_function(1,2,3,4,5)
and get calling another_function(1,2,3,4,5)
I know that I shoud use func_get_args()
to get all function arguments as array, but I don't know how to pass this arguments to another function.
Thank you.
To get the number of arguments that were passed into your function, call func_num_args() and read its return value. To get the value of an individual parameter, use func_get_arg() and pass in the parameter number you want to retrieve to have its value returned back to you.
You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside. function sendemail($id, $userid){ // ... } sendemail(array('a', 'b', 'c'), 10);
Separate parameters by a comma ( , ). Since PHP 8.0, the parameter list can have the trailing comma ( , ) which the PHP interpreter ignores. By default, arguments are passed by value in PHP. Prepend parameters by an ampersand ( & ) to pass arguments by reference.
Try call_user_func_array
:
function my_function() { $args = func_get_args(); call_user_func_array("another_function", $args); }
In programming and computer science, this is called an apply function.
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