有没有办法让一个函数接受的参数的数目不定? [英] Is there a way to allow a function to accept an indefinite number of arguments?

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

问题描述

我有可能能够处理成千上万的参数的函数。目前,我使用数组插入到它,但我认为这将是更友好有参数的数目不定。这可能吗?

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天全站免登陆