有没有办法允许函数接受无限数量的参数? [英] Is there a way to allow a function to accept an indefinite number of arguments?

查看:29
本文介绍了有没有办法允许函数接受无限数量的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以接受数百万个参数的函数.目前,我正在使用一个数组来插入它,但我认为拥有无限数量的参数会更友好.这可能吗?

I have a function that can potentially accept millions of arguments. Currently, I am using an array to insert into it, but I think it would be more friendly to have an indefinite number of arguments. Is this possible?

推荐答案

func_get_args() 就是你要找的

func_get_args() is what you're looking for

function sumValues() {
   $result = 0;
   foreach(func_get_args() as $arg) {
      $result += $arg;
   }
   return $result;
}

echo sumValues(1,2,3,4);

这篇关于有没有办法允许函数接受无限数量的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆