見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);
編輯1
當你調用foo(17, 20, 31)
func_get_args()
不知道的是,第一個參數表示例如$first
變量。當你知道每個數字索引代表着你可以做到這一點(或類似):
function bar()
{
list($first, $second, $third) = func_get_args();
return $first + $second + $third;
}
echo bar(10, 21, 37); // Output: 68
如果我想要一個特定的變量,我可以ommit別人一個:
function bar()
{
list($first, , $third) = func_get_args();
return $first + $third;
}
echo bar(10, 21, 37); // Output: 47
請訪問http:// PHP .net/manual/en/function.func-get-args.php – 2012-07-14 02:04:00