See func_get_args
:
function foo() { $numArgs = func_num_args(); echo 'Number of arguments:' . $numArgs . "\n"; if ($numArgs >= 2) { echo 'Second argument is: ' . func_get_arg(1) . "\n"; } $args = func_get_args(); foreach ($args as $index => $arg) { echo 'Argument' . $index . ' is ' . $arg . "\n"; unset($args[$index]); } } foo(1, 2, 3);
EDIT 1
When you call foo(17, 20, 31)
func_get_args()
, you don't know that the first argument is the $first
variable, for example. When you know what each numerical index is, you can do this (or the like):
function bar() { list($first, $second, $third) = func_get_args(); return $first + $second + $third; } echo bar(10, 21, 37);
If I want a specific variable, I can omit others:
function bar() { list($first, , $third) = func_get_args(); return $first + $third; } echo bar(10, 21, 37);
source share