Forum Moderators: coopster
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?
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.