« php: increase memory limit
xajax and radio buttons »

php: function with variable parameters

Categories: php

Tags: function, parameter, php

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() {
   for ($i 0$i func_num_args(); $i++) {
        echo "$i:" func_get_arg($i);
   }
}

alternative way to access the parameters:

PHP:

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

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)

Affliates

Spreadfirefox Affiliate Button