tags:

views:

532

answers:

10

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;
}
+3  A: 

I lean toward "less code is better" on the grounds that the result is easier to read and offers fewer places for bugs to hide.

Novelocrat
+5  A: 

Your second example hurts my head. It should probably read:

funciton displayApple2($str){
    if($str == 'apple')
        echo $str;
    return;
}

Personally, I don't use return statements if I am not specifically returning something.

BoltBait
I agree with BoltBait. Also, return statements can be used to exit a method early for some reason. ex. if($str != 'apple') return; followed by the rest of the method.
Peter Di Cecco
+13  A: 

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

McWafflestix
+5  A: 

You should not have a return statement in all functions.

When it does nothing it is just one more line of code.

slipbull
A: 

Well I don't know about returning a value on a "funciton", but what use it will have for you? Use it when you think this is good for your situation. Make a clean and useful code is a good practice too :)

Nathan
haha thanks for pointing out the funciton :P
justinl
A: 

[tangent]
I had a teacher dock me 5% on a test for not putting a return statement at the end of a void function in C.

Suffice to say I didn't take any more classes from her.
[/tangent]

Spencer Ruport
A: 

No, because Less Code = More Fun ^^

Btw, I believe that Functions without returns should be Subroutines.

GaiusSensei
A: 

I'll often return true on PHP functions to make error handling easier.

funciton displayApple2($str){
    if($str == 'apple')
        echo $str;
    if($str == 'orange')
        return false;
    return true;
}

if(!displayApple2($somestring))
    echo "You can't do that! Apples and oranges...";
cpharmston
Multiple returns like this make code harder to maintain.
Sohnee
+3  A: 

Only use a return when you need to, otherwise let the language do it's thing.

David Anderson
+1  A: 

If you don't return something from a C function, then the return value becomes whatever random value was previously in RAM at the time you called the function. This is undesirable because a function with no return appears to be returning random values. Therefore, in C you should always have a return statement in every non-void C function so you're not returning random garbage.

PHP doesn't have this problem - if you don't use a return statement, functions are guaranteed to return null, so it is better to leave them out and save some space.

too much php