Sometimes we need to create functions that require an indefinite and variable amount of arguments. Creating a function to cater for these arguments may seem straight-forward; I've seen several people who have created functions with five or so arguments and if they required a sixth argument at any stage during their project's development, they added the sixth argument to the function and carried on as normal.
Consider the function below that somebody has put together to construct an array from the arguments that are passed into the function:
php Code:
function create_array
($szItem1,
$szItem2,
$szItem3){ return array($szItem1,
$szItem2,
$szItem3);
}
This will work perfectly. However, you are not planning for future expansion. Suppose in a week's time you want to create an array with four arguments, what will you do then? Most people would simply add another argument and not look for a more long-term solution to the predicament.
Luckily, there is a fairly straight-forward solution, and that is to use a series of native PHP functions. You do not have to explicitly specify the arguments but rather add the arguments in the function call and let PHP do the rest. See what this looks like now we have adapted our code:
All this does is takes the arguments that we specified in the function call, and return them as an array. There's nothing more to it than that! As we do not know how many arguments we will be requiring,
func_get_args takes all the arguments and returns them as an array. Our function call looks like this:
php Code:
create_array(5, 'Orange', 'Apple', 'Grape');
We can add as many arguments as we like and the function will return us an array containing those arguments. To take this a step further, we can specify explicit arguments and interpret them differently. Even when using
func_get_args, we may still place arguments in to the function declaration, like so:
PHP Code:
function create_array($iLimit)
{
$aArgs = array();
for($iIndex = 1; $iIndex < func_num_args(); $iIndex++)
{
$szItem = func_get_arg($iIndex);
if(strlen($szItem) <= $iLimit)
{
$aArgs[] = $szItem;
}
}
return $aArgs;
}
What the above adaptation does is takes a mandatory argument. From that argument we are going to debar any fruit that has more than five characters. In our original function call,
apple and
grape will get through, whereas
orange will be discarded. Poor orange! A couple of new functions in the above code are
func_num_args which returns an integer value informing you how many arguments have been specified, and
func_get_arg which returns a single argument based on the argument offset you pass it. Incidentally, we begin the
$iIndex at 1 to bypass the
$iLimit argument. Our two out of three fruits are then returned as an array when we call the function:
php Code:
$aShortNamedFruits = create_array(5, 'Orange', 'Apple', 'Grape');
And all works well! Using these series of functions definitely spares you from having to return to your function add more arguments if and when you need them. By using
func_get_args you can specify limitless arguments and the function will carry on working regardless. If you're already wise enough and realise how to use
func_get_args then you'll no doubt be happy to never return to that function again to add extra arguments. It's up to you to spread the word - life just got a little bit easier!