PHP默认参数
在 PHP 中,如果我有一个像这样编写的函数
function example($argument1, $argument2="", $argument3="")
并且我可以在其他地方调用这个函数,就像这样
example($argument1)
但是如果我想给第三个参数一些值, 我应该这样做
example($argument1, "", "test");
还是
example($argument1, NULL, "test");
我认为两者都会起作用,但更好的方法是什么?因为将来如果在主函数中更改 $argument2 的值并且如果我有“”,那么会有什么问题吗?或者 NULL 是最好的选择?
谢谢
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)
But if I want to give some value to the thrid argument,
Should I do this
example($argument1, "", "test");
Or
example($argument1, NULL, "test");
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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为您不想指定的参数传递
null
或""
仍然会导致这些 null 和空字符串被传递给函数。仅当参数在调用代码中未设置全部时才使用参数的默认值。
example('a')
会让 args #2 和 #3 获得默认值,但如果你这样做example('a', null, "")
那么 arg #3 为 null,arg #3 是函数中的空字符串,而不是默认值。理想情况下,PHP 将支持诸如
example('a', , "")
之类的内容,以允许将默认值用于 arg #2,但这只是一个语法错误。如果您需要在函数调用过程中省略参数,但为后面的参数指定值,则必须显式检查函数本身中的某些标记值并自行设置默认值。
下面是一些示例代码:
对于各种输入:
请注意,一旦您在函数调用中为参数指定了 ANYTHING,该值就会传递到函数并覆盖函数定义中设置的默认值。
Passing
null
or""
for a parameter you don't want to specify still results in those nulls and empty strings being passed to the function.The only time the default value for a parameter is used is if the parameter is NOT SET ALL in the calling code.
example('a')
will let args #2 and #3 get the default values, but if you doexample('a', null, "")
then arg #3 is null and arg #3 is an empty string in the function, NOT the default values.Ideally, PHP would support something like
example('a', , "")
to allow the default value to be used for arg #2, but this is just a syntax error.If you need to leave off arguments in the middle of a function call but specify values for later arguments, you'll have to explicitly check for some sentinel value in the function itself and set defaults yourself.
Here's some sample code:
and for various inputs:
Note that as soon you specify ANYTHING for the argument in the function call, that value is passed in to the function and overrides the default that was set in the function definition.
您可以使用:
或者
您始终可以使用而不是空字符串来检查 NULL:
我认为这完全取决于带有参数的函数内部发生的情况。
You can use either:
Or
You can always check for NULL using instead of empty string:
I think it all depends on what happens inside the function with the args.
我认为如果您在函数定义中将可选参数定义为
null
并保持这种方式,从而将它们称为example($argument1, NULL, $argument="测试”)。只需确保如何在函数本身中使用 null 参数即可不引发任何警告。只要您确定如何在函数本身中使用该参数,您也可以使用
false
。如果你似乎经常遇到这个问题,更好的方法可能是对变量选项使用单个关联数组:
这样你就可以传入你想要的任何函数参数,保留任何其他参数,并确保定义了适当的默认值函数内。
唯一的缺点是预期的参数隐藏在函数签名中,但您可以将它们记录在函数的文档块中。
I think you will be better served if you define the optional parameters as
null
in the function definition and keep it that way, thus calling them asexample($argument1, NULL, $argument="test")
. Just be sure of how you use the null'd parameters in the function itself to not raise any warnings. You can usefalse
too as long as you're certain of how you use that parameter in the function itself.If you seem to get this issue alot, a better way may be to use a single associative array for the variable options:
That way you can pass in any function parameters you wish, leaving any others out and making sure there are the appropriate defaults defined within the function.
The only drawback is that the expected parameters are hidden from the function signature but you can document them in the function's docblock.
这取决于功能的规格。
执行
结果:假。但只是 a() 结果: true
这意味着您想要默认参数,那么您应该使用默认参数:
It depends on spesification of function.
executing
results : false. but simply a() results : true
That means you want default argument, then you should use as default: