Re: return
| From: | Steve Edberg | Date: | Mon, 11 Dec 2000 23:56:06 +0000 |
| Subject: | Re: return | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29770@lists.php.net to get a copy of this message | ||
At 6:02 PM -0300 12/11/00, Martin A. Marques wrote:
On Monday 11 December 2000 17:51, Louis Aslett wrote: When you execute a return(), the function exits and returns to the point at which the function was called in your code. Sorry if the question was rather stupid, but I found code in www.zend.com with functions with returns and after that a break.Do you mean something like this? function foo($bar) { if ($bar == 'zappa') { return 'frank'; } elseif ($bar == 'sinatra') { return 'Frank'; } elseif ($bar == 'valli') { return 'frankie'; } else { return; } } You can have as many return statements as you want; once the function hits a return statement, of course, any code after that in the function is not executed. You can even have NO return statement in a function definition; in that case, there is an implicit return just before the closing '}' in the function definition. Just as a matter of coding style, I like to only have one return statement in a function. It makes it easier for me to follow program flow while debugging --- and it's just the way I've always done it (with exceptions here & there). I would do the above like: function foo($bar) { if ($bar == 'zappa') { $rval = 'frank'; } elseif ($bar == 'sinatra') { $rval = 'Frank'; } elseif ($bar == 'valli') { $rval = 'frankie'; } return $rval; } -steve +--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+