9

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;
}
Charles
  • 48,924
  • 13
  • 96
  • 136
justinl
  • 9,764
  • 19
  • 67
  • 87
  • 1
    I couldn't find the answer to a question like this anywhere else on the site. Closest I found was something like this: http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – justinl Jul 30 '09 at 00:10
  • 1
    I'm not sure if this question is really specific to PHP. Do people think the answer to this is dependent on conventions that vary substantially from language to language? – Phil Miller Jul 30 '09 at 00:11

9 Answers9

17

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

aksu
  • 5,035
  • 5
  • 21
  • 38
Paul Sonier
  • 36,435
  • 3
  • 72
  • 113
5

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
  • 11,086
  • 9
  • 54
  • 84
  • 1
    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 Jul 30 '09 at 00:17
5

You should not have a return statement in all functions.

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

slipbull
  • 1,459
  • 1
  • 11
  • 27
3

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.

Phil Miller
  • 32,214
  • 11
  • 62
  • 86
3

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

David Anderson
  • 12,975
  • 5
  • 47
  • 72
2

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
  • 81,874
  • 33
  • 123
  • 133
0

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
  • 3,697
  • 1
  • 21
  • 20
0

[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
  • 34,215
  • 11
  • 81
  • 141
0

No, because Less Code = More Fun ^^

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

GaiusSensei
  • 1,846
  • 4
  • 25
  • 44