php函数中的动态参数
可能重复:php函数中的动态参数,php,function,dynamic,arguments,Php,Function,Dynamic,Arguments,可能重复: 那么 在java中,我可以这样做(伪代码): 然后: hello('first', 'second', 'no', 'matter', 'the', 'size'); 在php中是这样的吗 编辑 我现在可以传递一个数组,比如hello(array(bla,bla)),但是may可以通过上面提到的方式存在,对吗?请参见: 编辑1 例如,当调用foo(17,20,31)func\u get\u args()时,您不知道第一个参数表示$first变量。当您知道每个数字索引代表什么时,您
那么 在java中,我可以这样做(伪代码): 然后:
hello('first', 'second', 'no', 'matter', 'the', 'size');
在php中是这样的吗
编辑
我现在可以传递一个数组,比如hello(array(bla,bla))
,但是may可以通过上面提到的方式存在,对吗?请参见:
编辑1
例如,当调用foo(17,20,31)
func\u get\u args()
时,您不知道第一个参数表示$first
变量。当您知道每个数字索引代表什么时,您可以执行以下操作(或类似操作):
如果我想要一个特定的变量,我可以使用其他变量:
function bar()
{
list($first, , $third) = func_get_args();
return $first + $third;
}
echo bar(10, 21, 37); // Output: 47
现在你看,从PHP5.6开始,你可以使用…$params语法:你太快了,一个狂怒的人:D这就是我想要的。非常感谢。还有其他人,哈哈哈,不客气!我可以这样做:
func\u get\u args('argument\u name')
?获取参数的值。@FranciscoCorrales您不能func\u get\u args()
返回数值索引的参数数组,而不是具有相应对的变量列表。此答案已过时。由于PHP5.6,您可以使用
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);
function bar()
{
list($first, $second, $third) = func_get_args();
return $first + $second + $third;
}
echo bar(10, 21, 37); // Output: 68
function bar()
{
list($first, , $third) = func_get_args();
return $first + $third;
}
echo bar(10, 21, 37); // Output: 47