PHP得到的所有参数作为数组? [英] PHP get all arguments as array?
本文介绍了PHP得到的所有参数作为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是用PHP函数,有多个参数并格式化他们一起工作。目前,我有这样的工作:
Hey, I was working with a PHP function that takes multiple arguments and formats them. Currently, I'm working with something like this:
function foo($a1 = null, $a2 = null, $a3 = null, $a4 = null){
if ($a1 !== null) doSomethingWith($a1, 1);
if ($a2 !== null) doSomethingWith($a2, 2);
if ($a3 !== null) doSomethingWith($a3, 3);
if ($a4 !== null) doSomethingWith($a4, 4);
}
但我在想,如果我可以使用这样一个解决方案:
But I was wondering if I can use a solution like this:
function foo(params $args){
for ($i = 0; $i < count($args); $i++)
doSomethingWith($args[$i], $i + 1);
}
不过还是调用该函数以同样的方式,类似于C#中的params关键字或JavaScript中的参数数组。
But still invoke the function the same way, similar to the params keyword in C# or the arguments array in JavaScript.
推荐答案
func_get_args
返回与当前函数的所有参数数组。
func_get_args
returns an array with all arguments of the current function.
这篇关于PHP得到的所有参数作为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文