Where to return false in a PHP function?

it

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

equal to this

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

Thank!

+5
source share
11 answers

Yes, both do the same. Personally, I prefer to exit the function in the same place. eg.

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

In the scenario described above, Yes . Or it should work fine. I prefer the first method, less typing and that's it.

+8
source

, .

+3

.

:

function doThings(){
    return $oh;
}

, PHP "false", false, $oh, false, .

:

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

, , , ... , .

+1

, .

Quicktip:

:

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

, , . , ( ). .

+1

:

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

PHP:

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

, :

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

  // do stuff

  return $oh;
}
+1

:

, . - (, )

function doSomething() {
  $return_value = false;

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

  return $return_value;
}

, .

0

. return , , .

, - .

0
  • .
  • , .
0

, return , , , , . , , PHP .

0

All Articles