0

What is the best practice for placing a return value in PHP function that doesn't need to return anything? Here I have two examples here.

No Return

function SayHello(){
    echo 'Hello';
}

Return no value

function SayHello(){
    echo 'Hello';
    return;
}

Irrelevant...

function SayHello(){
    echo 'Hello';
    return; // or no return; - It really doesn't matter, there is no standard
}

If there really is no rule of thumb here, could you please let me know what PSR standards have to say about this (if anything)?

I'm just trying to form good habits. If this is opinion based, could you not flag this question as 'opinion based' - but simply answer 'opinion based'?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Jacob
  • 890
  • 10
  • 30
  • Haven't seen anyone leaving empty returns (except coffeescript). since return type void was added to PHP 7.1 I'm sure it's ok. https://wiki.php.net/rfc/void_return_type – JimL Jul 07 '17 at 18:41
  • I'm not sure what you mean JimL. Which would be ok? Or best? – Jacob Jul 07 '17 at 18:43
  • If you're using PHP 7.1 I would set the return type to void (for dev experience, ide auto completion/hinting and getting exceptions if the function is misused), but not have an actual return in the function. it serves no purpose – JimL Jul 07 '17 at 18:45
  • 1
    It's pointless noise to have an unnecessary `return;` statement. I'd think it was a typo/bug at first if I saw it in a PHP codebase. – ceejayoz Jul 07 '17 at 18:45
  • Could anyone cite a standard such as PSR that defines it one way or another? – Jacob Jul 07 '17 at 18:49
  • Until we cite a standard, I guess this is more of an opinion based answer. If you look at this answer: https://stackoverflow.com/questions/1218580/what-does-a-php-function-return-by-default#answer-1218598 - You can see that it doesn't matter which way you handle it on a coding level. Thanks for the help though. I think I'll take ceejayoz advice and drop it for now. – Jacob Jul 07 '17 at 18:51

0 Answers0