“退货”和“不退货”有什么区别?
两者之间有区别吗:
function someMethod( $someArg ) {
// some code
return;
}
和
function someMethod( $someArg ) {
// some code
// no return
}
两者都将 NULL
作为“返回值”。有区别吗? PHP 内部有什么东西吗?表现?速度?
编辑
我问,因为在 Zend 框架中(在此video)他们使用 return;
这对我来说似乎很愚蠢。然而,您可能会认为 Zend 框架背后的人确实了解他们的 PHP...
Is there a difference between:
function someMethod( $someArg ) {
// some code
return;
}
and
function someMethod( $someArg ) {
// some code
// no return
}
Both have NULL
as 'return value'. Is there a difference? Something PHP internally? Performance? Speed?
edit
I ask, because in Zend framework (in this video) they use return;
which seemed (seems) silly to me. However, you would think that the people behind Zend framework do know their PHP...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
php 代码
生成字节码
,因此显式 return 语句会生成一个额外的 RETURN 指令。否则没有什么区别。
php code
generated bytecode
so the explicit return statement generates one extra RETURN instruction. Otherwise there's no difference.
据我所知没有什么区别。
空的
return;
主要是为了从 if/else、while 或 for 循环中跳出来,而不返回任何内容。As far as I know there is no difference.
The empty
return;
is mainly there to break out from a if/else, while or for loop without returning anything.从功能上来说没有什么区别,但在函数中有一个明显的退出点(返回)总是好的。一些计算机科学流派认为所有函数和方法都应该有 1 个(而且只有 1 个)出口点。
如果您将来需要添加返回值,那么如果您包含空返回值,那么代码中已经为您挑选了一个明显的点。
但正如我所说,从功能角度来看,没有太大区别。
Functionally there's no difference but it's always nice to have an obvious point of exit in a function (the return). Some schools of computer science hold tht all functions and methods should have precisely 1, and only 1, point of exit.
Should you need to add a return value in the future then there's an obvious point already picked out for you in the code if you include an empty return.
But like I said, from a functional point of view there's not much difference.
在你的例子中,没有区别。
但是,
return
将终止该函数,因此您可以选择跳过return
语句之后的代码(这就是其目的)。在您的示例中,是否希望看到
返回
取决于个人喜好。此外,在某些语言(如 Perl)中,最后一个表达式“隐式”是返回值,因此您甚至不需要使用“return”。 (但这在 C++ 等其他语言中不起作用。)
In your example, no difference.
However,
return
will terminate the function, so you have the option to skip code after thereturn
statement (that's its purpose).In your example, it is personal preference if you like to see the
return
or not.Further, in some languages like Perl, the last expression is "implicitly" the return value, so you don't even need to use "return". (This won't work in other languages like C++, though.)
没有什么主要区别,您可以使用
return;
提前中断函数调用。无论哪种方式,它们都会返回 NULL,这严格适用于工作流程。There isn't a major difference, you can just use
return;
to break out of a function call early. They will both returnNULL
either way, it's strictly for workflow.在 PHP 和 Python 等语言中,没有真正的区别。
return;
可用于提前退出函数。但是,当选择的编程语言是 C/C++、Java、C# 时,就会有所不同,其中没有返回表示
void
In language such as PHP and Python there's no real difference.
return;
can be used to break out of the function prematurely.However, there is a difference when the programming language of choice is C/C++, Java, C#, where no return signifies a
void
我不是专家,但我会说,如果差异可以忽略不计;有人可能会说,你通过返回来耗尽了咬合;或返回 false;在函数的末尾。
之类的东西一起使用
通常我将 return 与or
,如果我不想返回任何内容或者我想返回 false 我只是不使用 return
I'm no expert but I would say that if there is a difference in negligible; one could argue that your using up bites by putting return; or return false; at the end of a function.
typically I use return with something like
or
and if i don't want to return anything or I want to return false I just don't use return
一旦调用
return
,函数调用就会终止。它就像一个休息。因此,如果您的返回位于中间,它将获得一些性能,因为函数的其余部分没有执行。对于其余的,使用显式返回而不是隐式返回通常更好(以可读的方式)。如果将返回值保留在函数中,您就会知道对该函数的调用可能需要返回值。
As soon as
return
is called, the function call is terminated. It acts like a break. So if your return is in the middle, it would gain some performance because the rest of the function isn't executed.For the rest, it is often better (in a readability manner) to use an explicit return in stead of a implicit one. If you keep the return in the function, you will know that a call to that function might expect a return value.
return 将中断/退出该函数(将中断视为 switch/case )
return will break/exit the function (think break as in switch/case)
我有时喜欢使用“空”
return
作为函数的最后一个语句,以友好地提醒该函数不会返回任何值。在 ADA 中,有
过程
和函数
。第一个不返回任何数据,它们只是执行一些语句。这些函数可以返回数据。因此,空的
return
就像“这是一个过程”。I sometimes like to use an "empty"
return
as the last statement of a function as a friendly reminder that the function is not expected to return any value.In ADA you have
procedures
andfunctions
. The first do not return any data, they just execute some statements. The functions can return data.So an empty
return
would be like 'this is a procedure'.非常有趣(我认为),有一个返回类型会带来很大的不同。三种返回
null
会引发三种不同的错误:我不会猜到这一点,因为所有错误都是运行时的,并且所有返回值在技术上都是
null
。Very interestingly (I think), having a return type makes a lot of difference. Three kinds of returning
null
throw three different errors:I would not have guessed that, since all errors are runtime and all return values are technically
null
.