Page 1 of 1

Functions with a variable number of arguments

Posted: Tue Oct 04, 2011 8:34 am
by MattSheppard
Hi everyone,

This is something I've seen done in other people's code, but the code has been too complex for me to understand; how do you code a function to accept a variable number of arguments, so that

Code: Select all

DoThisFunction($var1, $var2, $var3);
and

Code: Select all

DoThisFunction();
Would both be valid?

Thanks in advance,
Matt

Re: Functions with a variable number of arguments

Posted: Tue Oct 04, 2011 8:52 am
by VladSun
If you know the maximum number of arguments, then use optional variable declaration:

Code: Select all

function DoThisFunction($var1 = null, $var2 = null, $var3 = null)
if not use http://www.php.net/manual/en/function.func-get-args.php . Take a look at the example.

Re: Functions with a variable number of arguments

Posted: Tue Oct 04, 2011 8:55 am
by MattSheppard
That's brilliant, thank you very much! Just what I was looking for :)