PHP提供了3个函数()用于检索在函数中所传递的参数。
func_get_args() 返回一个提供给函数的所有参数的数组;
func_num_args() 返回提供给函数的参数数目;
func_get_arg() 返回一个来自参数的特定参数。
$array = func_get_args();
// 函数的所有参数的数组
$count = func_num_args();
// 函数的参数数目
$value = func_get_arg(argument_number);
// 来自参数的特定参数
这样的函数结果不能作为一个参数直接给其他函数使用。要把这些函数的结果当作参数使用,必须首先把函数的结果值赋给一个变量,然后再使用这个变量。
举个可变参数求和例子
<html>
<head>
<title> Example </title>
</head>
<body>
<?php
function countList()
{
if (func_num_args() == 0)
{ return false; }
else
{
$count = 0;
for($i=0; $i<func_num_args(); $i++)
{ $count += func_get_arg($i); }
return $count;
}
}
echo countList(1, 2, 3, 4);
// 输出"10"
?>
</body>
</html>