在 PHP 函数中返回 false 的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5651433/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:06:27  来源:igfitidea点击:

Where to return false in a PHP function?

phpfunctioncoding-style

提问by Mohammad

Does this

做这个

function doThings(){
  if($oh){
    return $oh;
  }
  return false;
}

equal this

等于这个

function doThings(){
  if($oh){
    return $oh;
  }else{
    return false;
  }
}

Thank you!

谢谢!

回答by Jim W.

In the scenario you outlined above, Yes. Either should work fine. I prefer the first method, less typing and all.

在您上面概述的场景中,Yes。要么应该工作正常。我更喜欢第一种方法,少打字等等。

回答by trickwallett

Yes, both do the same. Personally, I prefer to exit a function at the same place. e.g.

是的,两者都做同样的事情。就个人而言,我更喜欢在同一个地方退出一个函数。例如

function doThings(){
  $flag = false;
  if($oh){
    $flag = $oh;
  }
  return $flag;   
}

回答by Jakub Hampl

Yes.

是的。

Also they more or less equal to:

此外,它们或多或少等于:

function doThings(){
    return $oh;
}

This is not strictly true since PHP has other "falsey" values than false but if you want to return either $ohor an falsey value then this will work fine.

这并非严格正确,因为 PHP 具有除 false 之外的其他“falsey”值,但如果您想返回其中一个$oh或 falsey 值,那么这将正常工作。

Another way to write an equivalent function would be:

编写等效函数的另一种方法是:

function doThings(){
    return $oh ? $oh : false;
}

回答by Anodyne

Yes, those functions are exactly equivalent in behaviour.

是的,这些功能在行为上完全相同。

回答by Matthew

I'd write it like this:

我会这样写:

function doThings() {
  return $oh ?: false;
}

Or with earlier versions of PHP:

或者使用早期版本的 PHP:

function doThings() {
  return $oh ? $oh : false;
}

Or, depending on the function:

或者,取决于功能:

function doThings() {
  $oh = null; // set to some default;

  // do stuff

  return $oh;
}

回答by Wesley van Opdorp

Yes they are the same.

是的,它们是一样的。

Quicktip:

小建议:

I prefer the following:

我更喜欢以下内容:

function do() {
    if (!$oh) {
      return false;
    }
    return $oh;
}

This way you do not have the extra bracets as in your second example. But you always know where your value is returned (at the end of the function). This makes it quicker to locate and browse functions.

这样你就没有第二个例子中的额外括号。但是你总是知道你的值在哪里返回(在函数的末尾)。这样可以更快地定位和浏览功能。

回答by krs1

To answer this question we must first question who we are as individuals and as a people... just kidding, for all intensive purposes they're the same.

要回答这个问题,我们必须首先质疑我们作为个人和民族的身份……开玩笑,就所有密集目的而言,它们都是一样的。

回答by Andy

Mo:

莫:

As already stated, the two functions do exactly the same. You might also want to try doing something like this ( there cases where this might be beneficial )

如前所述,这两个函数的作用完全相同。您可能还想尝试做这样的事情(在某些情况下这可能是有益的)

function doSomething() {
  $return_value = false;

  if($oh) {
    $return_value = true;
  }

  return $return_value;
}

This doesnt do anything special, it's just a reorganisation.

这没有什么特别的,它只是一个重组。

回答by Merlin

Both do the same. Once the return statement is encountered, the control is passed back to the code which calls this function.

两者都做同样的事情。一旦遇到 return 语句,控制权将传递回调用此函数的代码。

First one is preferred over the second one, because of less code.

第一个比第二个更受欢迎,因为代码更少。

回答by user2024102

  1. both having same behavior.
  2. you can place depends on your scenario.
  1. 两者都有相同的行为。
  2. 你可以放置取决于你的场景。