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屋!
查看全文