在 PHP 中,您可以调用不传入任何参数的函数,只要参数具有如下所示的默认值:
function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
echo "$t1, $t2, $t3";
}
test();
但是,假设我希望最后一个参数不同,但前两个参数应使用其默认值。我能想到的唯一方法是这样做但没有成功:
test('test1','test2','hi i am different');
我试过这个:
test(,,'hi i am different');
test(default,default,'hi i am different');
有干净、有效的方法来做到这一点吗?
使用数组:
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'));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)