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;
   }
}

Affliates

Spreadfirefox Affiliate Button