Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to return false in a PHP function?

Does this

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

equal this

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

Thank you!

like image 731
Mohammad Avatar asked Apr 13 '11 15:04

Mohammad


2 Answers

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;   
}
like image 175
trickwallett Avatar answered Oct 09 '22 03:10

trickwallett


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

like image 9
Jim Avatar answered Oct 09 '22 03:10

Jim