PHP的默认参数 [英] php default arguments
问题描述
在PHP,如果我写了这样的功能。
In PHP, if I have a function written like this
function example($argument1, $argument2="", $argument3="")
我可以这样调用这个函数在其他地方
And I can call this function in other place like this
example($argument1)
但是,如果我想给一些价值的THRID说法,
我应该这样做。
But if I want to give some value to the thrid argument, Should I do this
example($argument1, "", "test");
或者
example($argument1, NULL, "test");
我觉得这两个工作,但有什么更好的办法?因为在未来,如果为$参数2的值主要功能是改变,如果我有,那么会不会有什么问题?或NULL是最好的选择?
I think both will work but what is the better way? Because in future if the value for the $argument2 is changed in the main function and if I have "", then will there be any problem? Or NULL is the best option?
感谢
推荐答案
您可以使用:
example($argument1, '', 'test');
或者
example($argument1, NULL, 'test');
您可以随时查询使用,而不是空字符串NULL:
You can always check for NULL using instead of empty string:
if ($argument === NULL) {
//
}
我觉得这一切都取决于与ARGS函数内部发生了什么。
I think it all depends on what happens inside the function with the args.
这篇关于PHP的默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!