Forum Moderators: coopster

Message Too Old, No Replies

Function Writing Best Practices: Return or Echo

the quest for flexible, fast, and friendly functions

         

elbowlobstercowstand

12:22 am on Nov 9, 2007 (gmt 0)

10+ Year Member



Now is the time of the day where I over-analyze things , re-write hundreds of lines of code, and then second guess myself completely (and the hours I just spent making it "right"). Join me in my mental meanderings of the "best" way to write functions. Which do you think is a better practice, echoing or returning?

Echoing


function many_echos() {
echo "Ostriches are cool.";
echo "They lay big eggs.";
echo "Shipping one to a person's house would create a problem.";
}

many_echos();
//this would echo my ostrich opinions

Returning


function many_returns() {
$ret = "Ostriches are cool.";
$ret .= "They lay big eggs.";
$ret .= "Shipping one to a person's house would create a problem.";
return $ret;
}

echo many_returns();
//this would echo my ostrich opinions

I just rewrote my function library/classes to use the return method. Rationale:
1) when looking at the masters code (predefined functions in PHP), it seems they always return things.
2) Returning makes more semantical sense... because you might not always be echoing... sometimes you want to take the output of a function, store it another variable, do some crap to it, then echo it.

I'm also concerned with the performance hit I might have just taken as well. Like, does one method eat up way more memory than the other?

eelixduppy

12:50 am on Nov 9, 2007 (gmt 0)



The two types of methods you are describing are the Procedure vs the Function. As you may have already guessed, a procedure performs a task and does not return anything, whereas a function performs a task that has a return value. Now the one you choose really depends on what the method is going to perform, and how and where you are going to use the method.

Many methods that perform some sort of mathematical operation almost always are functions that return the result. This is because the result is usually used in some other form somewhere else in the script itself. There are of course other types of methods that would need to return a value.

There are methods, though, that would be procedures and not return anything at all. I know this isn't PHP related, but let's say you have a paint method that repaints a gui window each time it is dragged across the screen or resized. The method would not have to return anything, just change some variables within its class and maybe call some other methods.

So taking your example from above to be the whole method, I would say that you would want a procedure because there really isn't much you would do with the string unless you wanted to manipulate it in the main script somewhere, which if that were the case, you should have probably done that in the method to begin with. So it's really up to what you need the method to do that determines whether or not you should have a return statement or not. Unless given a specific example of a method that you don't know what to do with, I cannot give specific advice. Hope this helps, though.

elbowlobstercowstand

12:08 am on Nov 11, 2007 (gmt 0)

10+ Year Member



eelixduppy: Thanks so much for your reply. That totally makes sense. I had the desire to say: "I'll always do this or that..." because I'm a black and white type person. However, you are right... whether you echo or return completely depends on use of the function.

Thanks for the time and thoughts!