PHP 函数默认返回什么?
如果我没有显式返回任何内容,那么 php 函数到底返回什么?
function foo() {}
它是什么类型?
它是什么值?
如何使用 === 精确测试它?
这是否从 php4 更改为 php5?
function foo() {}
和function foo() { return; 之间有区别吗? }
(我不是问如何像 if (foo() !=0) ...
那样测试它
If I return nothing explicitly, what does a php function exactly return?
function foo() {}
What type is it?
What value is it?
How do I test for it exactly with === ?
Did this change from php4 to php5?
Is there a difference between
function foo() {}
andfunction foo() { return; }
(I am not asking how to test it like if (foo() !=0) ...
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在指定函数返回类型时,我确实发现了一个奇怪的地方。 当你这样做时,你必须明确从你的函数中返回一些东西。
错误:
因此,如果您决定在旧函数上添加一些返回类型规范,请务必考虑这一点。
I did find an oddity when specifying function return types. When you do, you must be explicit about returning something from your functions.
Error:
So if you decide to add some return type specifications on old functions make sure you think about that.
PHP 函数不返回值与返回 null 的函数具有相同的语义。
这将输出
You get the same result if foo is Replaced with
There has been nochange in this behavior from php4 to php5 to php7 (I just 经过测试可以肯定!)
Not returning a value from a PHP function has the same semantics as a function which returns null.
This will output
You get the same result if foo is replaced with
There has been no change in this behaviour from php4 to php5 to php7 (I just tested to be sure!)
null
null
if(foo() === null)
您可以通过执行以下操作来尝试:
null
null
if(foo() === null)
You can try it out by doing: