PHP将所有参数作为数组获取?

标签 php arrays function arguments

嘿,我正在使用一个 PHP 函数,它接受多个参数并设置它们的格式。目前,我正在处理这样的事情:

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);
}

但我想知道我是否可以使用这样的解决方案:

function foo(params $args){
    for ($i = 0; $i < count($args); $i++)
        doSomethingWith($args[$i], $i + 1);
}

但仍然以相同的方式调用函数,类似于 C# 中的 params 关键字或 JavaScript 中的参数数组。

最佳答案

func_get_args返回一个包含当前函数所有参数的数组。

关于PHP将所有参数作为数组获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/828709/

相关文章:

c++ - 读取一个文本文件并存入两个不同的数组 : a string array and float array

c# - 如何将字符串数组转换为长数组?

string - Delphi 汇编函数返回长字符串

python - Python 中的全局变量

javascript - 为多个 HTML 元素应用 .click() 函数并更改上下文

php - 删除数组中的重复值

php - 从外部网站获取所有脚本

php - 允许用户将图像上传到应用程序相册的 Facebook 应用程序

php - mysql/php随机

c - 是否可以对字符数组进行位掩码