可以按照声明中定义的不同顺序传递php函数参数 [英] can php function parameters be passed in different order as defined in declaration
本文介绍了可以按照声明中定义的不同顺序传递php函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个函数/方法F(),它接受3个参数$ A,$ B和$ C定义.
Suppose I have a function/method F() that takes 3 parameters $A, $B and $C defined as this.
function F($A,$B,$C){
...
}
假设我不想遵循传递参数的命令,我可以这样打吗?
Suppose I don't want to follow the order to pass the parameters, instead can I make a call like this?
F($C=3,$A=1,$B=1);
代替
F(1,2,3)
推荐答案
绝对不是.
您可以传递无序参数的一种方法是采用关联数组:
One way you'd be able to pass in unordered arguments is to take in an associative array:
function F($params) {
if(!is_array($params)) return false;
//do something with $params['A'], etc...
}
然后您可以像这样调用它:
You could then invoke it like this:
F(array('C' => 3, 'A' => 1, 'B' => 1));
这篇关于可以按照声明中定义的不同顺序传递php函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文