“退货”和“不退货”有什么区别?

发布于 2024-11-07 20:39:38 字数 516 浏览 0 评论 0原文

两者之间有区别吗:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

梦里南柯 2024-11-14 20:39:38

php 代码

<?php

function a() {
   echo 1;
   return;
}

function b() {
   echo 2;
}

生成字节码

.FUNCTION a
        ECHO                     1
        RETURN                   NULL
        RETURN                   NULL
        HANDLE_EXCEPTION         
.END FUNCTION

.FUNCTION b
        ECHO                     2
        RETURN                   NULL
        HANDLE_EXCEPTION         
.END FUNCTION

,因此显式 return 语句会生成一个额外的 RETURN 指令。否则没有什么区别。

php code

<?php

function a() {
   echo 1;
   return;
}

function b() {
   echo 2;
}

generated bytecode

.FUNCTION a
        ECHO                     1
        RETURN                   NULL
        RETURN                   NULL
        HANDLE_EXCEPTION         
.END FUNCTION

.FUNCTION b
        ECHO                     2
        RETURN                   NULL
        HANDLE_EXCEPTION         
.END FUNCTION

so the explicit return statement generates one extra RETURN instruction. Otherwise there's no difference.

请帮我爱他 2024-11-14 20:39:38

据我所知没有什么区别。

空的 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.

黎夕旧梦 2024-11-14 20:39:38

从功能上来说没有什么区别,但在函数中有一个明显的退出点(返回)总是好的。一些计算机科学流派认为所有函数和方法都应该有 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.

旧夏天 2024-11-14 20:39:38

在你的例子中,没有区别。

但是,return 将终止该函数,因此您可以选择跳过 return 语句之后的代码(这就是其目的)。

在您的示例中,是否希望看到返回取决于个人喜好。

此外,在某些语言(如 Perl)中,最后一个表达式“隐式”是返回值,因此您甚至不需要使用“return”。 (但这在 C++ 等其他语言中不起作用。)

sub foo {
  return 12;
}

sub foo2 {
  12;  # same thing
}

In your example, no difference.

However, return will terminate the function, so you have the option to skip code after the return 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.)

sub foo {
  return 12;
}

sub foo2 {
  12;  # same thing
}
沉溺在你眼里的海 2024-11-14 20:39:38

没有什么主要区别,您可以使用 return; 提前中断函数调用。无论哪种方式,它们都会返回 NULL,这严格适用于工作流程。

There isn't a major difference, you can just use return; to break out of a function call early. They will both return NULL either way, it's strictly for workflow.

栖竹 2024-11-14 20:39:38

在 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

糖果控 2024-11-14 20:39:38

我不是专家,但我会说,如果差异可以忽略不计;有人可能会说,你通过返回来耗尽了咬合;或返回 false;在函数的末尾。

之类的东西一起使用

return true;

通常我将 return 与or

return $var;

,如果我不想返回任何内容或者我想返回 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

return true;

or

return $var;

and if i don't want to return anything or I want to return false I just don't use return

笨死的猪 2024-11-14 20:39:38

一旦调用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.

风渺 2024-11-14 20:39:38

return 将中断/退出该函数(将中断视为 switch/case )

function someMethod( $someArg ) {
    if(condition) {
        return; // will exit function
    }

    // could be more code
}

return will break/exit the function (think break as in switch/case)

function someMethod( $someArg ) {
    if(condition) {
        return; // will exit function
    }

    // could be more code
}
つ低調成傷 2024-11-14 20:39:38

我有时喜欢使用“空”return 作为函数的最后一个语句,以友好地提醒该函数不会返回任何值。

class foo {
  var $name;

  public function bar($x) {
    $this->name = $x;
    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.

class foo {
  var $name;

  public function bar($x) {
    $this->name = $x;
    return;
  }
}

In ADA you have procedures and functions. 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'.

倦话 2024-11-14 20:39:38

非常有趣(我认为),有一个返回类型会带来很大的不同。三种返回 null 会引发三种不同的错误:

function foo() : string {
  // return nothing

  // TypeError exception: Return value of foo() must be of the type string, none returned
}

function foo() : string {
  return;

  // Fatal error: A function with return type must return a value
}

function foo() : string {
  return null;

  // TypeError exception: Return value of foo() must be of the type string, null returned
}

我不会猜到这一点,因为所有错误都是运行时的,并且所有返回值在技术上都是 null

Very interestingly (I think), having a return type makes a lot of difference. Three kinds of returning null throw three different errors:

function foo() : string {
  // return nothing

  // TypeError exception: Return value of foo() must be of the type string, none returned
}

function foo() : string {
  return;

  // Fatal error: A function with return type must return a value
}

function foo() : string {
  return null;

  // TypeError exception: Return value of foo() must be of the type string, null returned
}

I would not have guessed that, since all errors are runtime and all return values are technically null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文