php 函数是否应该使用:return null;?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8749091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should a function use: return null;?
提问by PeeHaa
Should a function return null
?
函数应该返回null
吗?
E.g.
例如
function test()
{
return null; // vs return;
}
Is the latter considered bad practice or doesn't it matter?
后者被认为是不好的做法还是无关紧要?
PS
聚苯乙烯
Whether it is bad practice shouldn't be subjective IMHO.
是否是不好的做法不应该是主观的恕我直言。
采纳答案by ThiefMaster
If you don't return anything, just use return;
or omit it at all at the end of the function.
If your function is usually returns something but doesn't for some reason, return null;
is the way to go.
如果您不返回任何内容,只需return;
在函数末尾使用或完全省略它。
如果您的函数通常返回某些内容但由于某种原因没有返回,return null;
则是要走的路。
That's similar to how you do it e.g. in C: If your function doesn't return things, it's void
, otherwise it often return either a valid pointer or NULL.
这与您在 C 中的做法类似:如果您的函数不返回内容,则返回void
,否则它通常返回有效指针或 NULL。
回答by devdRew
Always is a good practice to show what you're returning.
始终是展示您返回的内容的好习惯。
But as a tip, the following are all equivalent:
但作为提示,以下都是等效的:
function test()
{
return null;
}
function test()
{
return;
}
function test()
{
}
In all cases there for var_dump(test());
will be:
在所有情况下var_dump(test());
都会有:
NULL
回答by hakre
An undefined function return value in PHP always equals NULL
, so it does not make any difference (the runtime does this).
PHP 中未定义的函数返回值始终等于NULL
,因此没有任何区别(运行时会这样做)。
What makes a difference is that you make use of docblocks and document the return value with the @return
tag so clever IDE's offer info here.
有什么不同之处在于您使用文档块并使用@return
标签记录返回值,如此聪明的 IDE 在这里提供信息。
If you want to signal per the docblock that not-returning a value is intended, you can use void
:
如果您想根据文档块发出不返回值的信号,您可以使用void
:
* @return void
If you want to signal that returning NULL
is intended, you can use null
or NULL
(depending on your code-style for uppercase PHP standard constants):
如果您想表示NULL
打算返回,您可以使用null
或NULL
(取决于您的大写 PHP 标准常量的代码样式):
* @return null
or:
或者:
* @return NULL
That should make the coders intention visible as by PHP this is would be always null
as the factual return value.
这应该使编码人员的意图可见,因为 PHP 这将始终null
作为事实返回值。
Read on:
继续阅读:
More details and updated information is in PeeHaa's answer to this same question.
更多细节和更新信息在 PeeHaa 对同一问题的回答中。
回答by PeeHaa
It is just plain silly to return null
if there is nothing to return. Doing return;
shows intend of not actually returning anything. And makes it clear what the function does.
null
如果没有什么可以返回,返回是很愚蠢的。做return;
节目的目的是不实际返回任何东西。并明确该函数的作用。
The fact that PHP does not have a void
return type yet should not be a reason to make the intend less clear. Also note that PHP 7.1 will provide support for the void
return type:
PHP 没有void
返回类型这一事实不应成为使意图不那么明确的理由。还要注意的是PHP 7.1将提供支持的void
返回类型:
function test(): void {
return null; // this will error
}
回答by powtac
When you use return type declarationswith the nullable prefix "?" there is a difference!
当您使用带有可为空前缀“?”的返回类型声明时 它们是有区别的!
The PHP documentation says ...
PHP 文档说...
As of PHP 7.1.0, return values can be marked as nullable by prefixing the type name with a question mark (?). This signifies that the function returns either the specified type or NULL.
从 PHP 7.1.0 开始,可以通过在类型名称前加上问号 (?) 来将返回值标记为可为空。这表示该函数返回指定类型或 NULL。
So, when you return nothing the error message will be:
因此,当您不返回任何内容时,错误消息将是:
Fatal error: Uncaught TypeError: Return value of test() must be of the type string or null, none returned in ...
Fatal error: Uncaught TypeError: Return value of test() must be of the type string or null, none returned in ...
And when you return return;
the error message will be:
当您返回return;
错误消息时,将是:
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in ...
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in ...
When you return null
, everything is fine.
当你回来时null
,一切都很好。
回答by Jonah Bishop
Semantically, I don't think there's a difference between the two. According to the PHP documentation on return:
从语义上讲,我认为两者之间没有区别。根据返回的PHP 文档:
If no parameter is supplied, then the parentheses must be omitted and NULL will be returned.
如果未提供参数,则必须省略括号并返回 NULL。
I personally like putting the NULL there. Then there's no question as to what's being returned, making your debugging a little easier.
我个人喜欢把 NULL 放在那里。那么对于返回的内容毫无疑问,使您的调试更容易一些。
回答by Elijah Lynn
The PEAR best practices page does a return null
in their "Return early" example. This increases readability which we should always try to do since we write code for humans, not computers.
PEAR 最佳实践页面return null
在他们的“尽早返回”示例中做了一个。这增加了可读性,我们应该始终尝试这样做,因为我们为人类而不是计算机编写代码。
source: http://pear.php.net/manual/en/standards.bestpractices.php
来源:http: //pear.php.net/manual/en/standards.bestpractices.php