RE: [PHP] variable num of function args
| From: | Ford, Mike [LSS] | Date: | Tue, 03 Dec 2002 15:01:05 +0000 |
| Subject: | RE: [PHP] variable num of function args | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-126863@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: christian haines [mailto:cmh@chariot.net.au]
> Sent: 03 December 2002 13:32
>
>
> hi all,
>
> is it possible to somehow have a function which takes a
> variable number
> of arguments
Yes -- look at the manual pages for func_num_args (http://www.php.net/func-num-args) and
func_get_arg (http://www.php.net/func-get-arg).
> (with some kind of key association)
No.
> i am sick of continually having to go back and add empty
> parameters to
> functions in use.
Why don't you just make the extra parameters optional? As in:
function test1($test, $this=NULL) {
if (isset($this)):
echo "\$this was passed with value $this";
else:
echo "\$this was not passed";
endif;
}
> function test2($arr)
> {
> extract($arr);
> print $this;
> }
>
> test2($arr = array(
> "test" => 1,
> "this" => 2
> ));
> // output
> 2
>
> is there another ... better or cleaner way?
If you want "named" parameters, this is pretty much how to go about it -- except that the
function call doesn't need the "$arr =", so could be written more cleanly as:
test2( array('test'=>1, 'this'=>2) );
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211