Take the 2-minute tour ×

Let say i have function:

function myFunction($param1, $param2, /*0 or more extra args*/) {
  // do something with $param1 & $param2
  $args = func_get_args();
  $other_args = array_slice($args, 2); // get the dynamic args
  // do something with $other_args

this enables to call:

myFunction($param1, $param2); // or
myFunction($param1, $param2, $param3); // or
myFunction($param1, $param2, $param3, $param4);

But i want to know if it possible to to take an array of args and pass them into the function e.g:

$extra_args = array($param3, $param4); // this could also be
$extra_args = array($param3, $param4, $param5); // or any length array

i would like to pass them as separate params into my function e.g

myFunction($param1, $param2 /* other args split up */);

Thanks in advance!

share|improve this question
What have you tried? You want to call your function with a array of params? –  Gabriel Santos Jul 13 '12 at 18:18
yea, that's what i want to do –  Lizard Jul 13 '12 at 18:20
This question seems very basic. Yes, you can pass a param of an array to the function as long as you build the function to handle it. –  Jeremy Jul 13 '12 at 18:21
@Jeremy - he doesn't want to pass an array, he wants to use an array and have it passed as though each element in the array was passed as its own function parameter, like JavaScript's function.prototype.apply() method (if that helps). –  Utkanos Jul 13 '12 at 18:35
Why does it have to be called as if all the args were passed as opposed to putting them in an array and handling them that way? I dont see why thats necessary. I do agree that your solution does match what the person is asking though @Utkanos –  sbditto85 Jul 13 '12 at 18:45
show 4 more comments

2 Answers

up vote 3 down vote accepted

Yeap - call_user_func_array()'.


Takes two arguments - the function name (as a string - or possibly, in PHP >= 5.3, an anonymous function, not sure) and an array of params.

Or for methods, forward_static_call_array()


PHP's answer to JS's function.prototype.apply() :)

share|improve this answer
call_user_func_array will probably work for my scenario - can't believe I didn't think of that :) Also didn't know of forward_static_call_array - but unfortunately the function is static - good to know though. –  Lizard Jul 13 '12 at 18:25
add comment
share|improve this answer
add comment

Your Answer


