无限论据PHP函数? [英] Unlimited arguments for PHP function?

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

问题描述

在PHP中你将如何创建一个可以采取无限数量的参数的函数: myFunc的($ P1,P2 $,$ P3,P4 $,$ P5 ...);

我的下一个问题是:你会如何将它们传递到另一个功能类似于

 函数myFunc的($ params)方法{
  anotherFunc($ params)方法;
}

anotherFunc 将接受他们,如果它是使用名为 anotherFunc($ P1,$ P2,$ P3,$ P4,$ P5 ...)


解决方案

  call_user_func_array('anotherFunc',func_get_args());

func_get_args 返回包含传递给它从调用的函数,并且所有的参数 call_user_func_array 调用一个给定的数组函数,传入的参数数组。

In php how would you create a function that could take an unlimited number of parameters: myFunc($p1, $p2, $p3, $p4, $p5...);

My next question is: how would you pass them into another function something like

function myFunc($params){
  anotherFunc($params);
}

but the anotherFunc would receive them as if it was called using anotherFunc($p1, $p2, $p3, $p4, $p5...)

解决方案

call_user_func_array('anotherFunc', func_get_args());

func_get_args returns an array containing all arguments passed to the function it was called from, and call_user_func_array calls a given function, passing it an array of arguments.

这篇关于无限论据PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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