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:
- func_num_args() – returns number of arguments or parameters
- func_get_arg($i) – get specific argument on the list
- func_get_args() – returns array of arguments passed
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; | |
} | |
} |
2008-10-29 11:26:00 • Link • Comments • Trackbacks
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
