php: function with variable parameters

by prettyscripts on 2008-10-29 11:26:00

phpcode

to define a function that takes any number of parameters, eg

PHP:

foo(); // no parameter
foo("a"); // 1 parameter
foo("a""b""c"); // more than 1 parameters

to access the parameters passed to a function, use these php functions:

PHP:

function foo() {
   foreach (func_get_args() as $i => $arg) {
        echo "$i:" $arg;
   }
}

Tags: function, php

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)