开发者

Is there a way to allow a function to accept an indefinite number of arguments?

开发者 https://www.devze.com 2023-02-05 01:50 出处:网络
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 numb

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() 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消