doubledee Posted June 9, 2012 Share Posted June 9, 2012 Some questions about the return statement in a Function... 1.) Do you have to assign what is being returned to a variable, before you can use the return statement. For example, must you do this... $postsPerDay = round(($numberOfPosts/$daysAsMember), 3); return $postsPerDay; Or is it acceptable to do this... return round(($numberOfPosts/$daysAsMember), 3); 2.) Can you have return in multiple places in your Function? For example, can you do this... if ($estimatedAge < 20){ return 'Youth'; }else if ($estimatedAge < 30){ return '20-something'; }else if ($estimatedAge < 40){ return '30-something'; }else{ return 'Senior'; } Or do you have to do this... if ($estimatedAge < 20){ $ageRange = 'Youth'; }else if ($estimatedAge < 30){ $ageRange = '20-something'; }else if ($estimatedAge < 40){ $ageRange = '30-something'; }else{ $ageRange = 'Senior'; } return $ageRange; Thanks, Debbie Link to comment https://forums.phpfreaks.com/topic/263892-questions-about-return-in-function/ Share on other sites More sharing options...
trq Posted June 9, 2012 Share Posted June 9, 2012 1) No, you do not have to store the value in a variable before retuning it. 2) Yes, just be aware that return exits the function Link to comment https://forums.phpfreaks.com/topic/263892-questions-about-return-in-function/#findComment-1352362 Share on other sites More sharing options...
PeoMachine Posted June 9, 2012 Share Posted June 9, 2012 On the second question... i think the second way you did that is the better way to do. i think it improve the reading and the sense of the code. Link to comment https://forums.phpfreaks.com/topic/263892-questions-about-return-in-function/#findComment-1352371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.