Re: Variable number of parameters

From: Date: Tue, 05 Aug 2008 15:05:56 +0000
Subject: Re: Variable number of parameters
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-277725@lists.php.net to get a copy of this message
On Aug 5, 2008, at 8:58 AM, Daniel Brown wrote:
On Tue, Aug 5, 2008 at 1:40 AM, Philip Thompson <philthathril@gmail.comwrote:
Is it possible to grab a variable number of parameters and send the appropriate amount to another function?
You can predefine function parameters: <?php function example($line1,$line2="There is no line 2.") { echo $line1."\n"; echo $line2."\n"; } example("This is a test.","This is another test."); /* Returns:
       This is a test.
       This is another test.
*/ example("This is a third test."); /* Returns:
       This is a third test.
       There is no line 2.
*/ ?> .... but more likely, you're looking for func_get_args().
       http://php.net/func_get_args
Fortunately, I am quite familiar with your example. However, I am more looking for something in which I could send any number of parameters to my own function, grab each one of those individually, and then send them each to another built-in function. Basically, I want to avoid hard-coding this. On top of looking ugly, it's bad practice. <?php function myFunction () { $args = func_get_args(); anotherFunction ($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]); // And continue counting up to a random number where I don't // think I'll have more than, say, 10 arguments } ?> Of course, even if I did hard-code this nastiness, anotherFunction() would still complain about whatever parameters were not used. So, args 0 and 1 are filled, but 2-5 are not. "Warning! Warning! You can't have args 2 through 5, stoopud!" Alas, I feel I must take a different approach to solve this problem. ~Philip

« previous php.general (#277725) next »