调用函数时使用默认值 [英] Using default value when calling a function
本文介绍了调用函数时使用默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,您可以调用一个函数.通过不使用这些语句调用所有参数.
In PHP you can call a function. by not calling all parameters by using these statement.
function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
echo "$t1, $t2, $t3";
}
您就可以使用像这样的功能
And you can just use the function like this
test();
所以我们只想说我希望最后一个与众不同,而不是其他.我能做的唯一方法就是没有成功:
So lets just say I want the last one to be different but not the others. The only way I can thing of is by doing this with no success:
test('test1','test2','hi i am different');
我尝试过:
test(,,'hi i am different');
test(default,default,'hi i am different');
做这样的事情的最好方法是什么?
What is the best way to do something like this?
推荐答案
使用数组:
function test($options = array()) {
$defaults = array(
't1' => 'test1',
't2' => 'test2',
't3' => 'test3',
);
$options = array_merge($defauts, $options);
extract($options);
echo "$t1, $t2, $t3";
}
以这种方式调用函数:
test(array('t3' => 'hi, i am different'));
这篇关于调用函数时使用默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文