Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good programming practice to have a return statement in all functions?

Tags:

function

php

I have a basic programming question. I would like to know if every non-void function should have an "return" statement in PHP script.

Take the following two example functions. Which one would be the better way to program? They both do the same thing (to my understanding) but which is the "better practice" and why?

function displayApple1($str){
    if($str == 'apple')
        echo $str;
}

function displayApple2($str){
    if($str == 'apple')
        echo $str;
    else
        return;
}
like image 775
justinl Avatar asked Jul 30 '09 00:07

justinl


1 Answers

Overuse of return is a bad thing. Your execution paths should be simple and straightforward; overuse of the return keyword can imply (improper) complexity.

like image 78
Paul Sonier Avatar answered Oct 14 '22 04:10

Paul Sonier