Take the 2-minute tour ×

I was wondering how in php you would create a function that could take an unlimited number of parameters,


you could call

myFunc($p1, $p2, $p3, $p4, $p5...);

as many as you like.

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...)

share|improve this question
php.net/manual/en/function.func-get-args.php most likely what you want. Define function with no arguments and use this to get what was passed at runtime. –  morphles Feb 15 '11 at 20:57
possible duplicate of Can a PHP function accept an unlimited number of parameters? –  JYelton Feb 15 '11 at 21:04
add comment

3 Answers

up vote 11 down vote accepted
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.

share|improve this answer
+1 func_get_args exists for exactly the situation where you need to pass an arbitrary number of arguments. –  Endophage Feb 15 '11 at 21:03
add comment

Is there a reason why you couldn't use 1 function argument and pass all the info through as an array?

share|improve this answer
because this is actually for a function in a loader class, you call load::library('name', $p1, $p2, $p3, $p4); it returns a reference to the new class where $p1... is passed to the constructor –  Hailwood Feb 15 '11 at 21:30
add comment

Check out the documentation for variable-length argument lists for PHP.

The second part of your question refers to variable functions:


share|improve this answer
add comment

Your Answer


