V Posted June 14, 2010 Share Posted June 14, 2010 I took somone's advice on a blog not to use "echo" in functions but "return" instead. So I replaced all echo with return and things weren't working properly anymore. For example code like, while ($row = mysql_fetch_array($result)) { echo "<option value=\"" . $row["cat_id"] . "\">"; echo $row["category_name"]; echo "</option>"; I use that within a function to have a dropdown list of categories in forms. After using "return" the list was empty. Now I'm not sure, was that guy totally wrong or am I doing something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/ Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 Well what did you replace that with? A suitable replacement would be something like the following: ... $result = ''; while ($row = mysql_fetch_array($result)) { $result .= "<option value=\"" . $row["cat_id"] . "\">"; $result .= $row["category_name"]; $result .= "</option>"; } return $result; ... echo function_name(); Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072142 Share on other sites More sharing options...
V Posted June 15, 2010 Author Share Posted June 15, 2010 Oh, I replaced the 3 echos with return within the function. The code you posted didn't even cross my mind. I'll implement it immediately thank you! edit: I used the code but once again the dropdown list appeared empty. It seems that only echo does the trick Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072146 Share on other sites More sharing options...
V Posted June 15, 2010 Author Share Posted June 15, 2010 ..but I still don't get it. Most examples I see use echo in function, like on here http://www.w3schools.com/PHP/php_functions.asp so why was my original code incorrect? Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072147 Share on other sites More sharing options...
DarnMeek Posted June 15, 2010 Share Posted June 15, 2010 I have never heard that echoing in a function is bad practice. I might be wrong though. Anyway I feel that that should depend on your application functionality and how lazy you are:D Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072150 Share on other sites More sharing options...
trq Posted June 15, 2010 Share Posted June 15, 2010 Your original code isn't incorrect, but generally its bad practice to echo data or even produce html within functions unless of course that is the actual intention of the function. For instance, a function called getCatigorys(), should return an array of data, not a html drop down. What if you want to use the same functionality within a script that produces XML? However, you could have another function called arrayToDropDown() that is specifically designed to make a dropdown from an array. Combing these two functions now gives you all the functionality you need to make a dropdown from a simple query while still keeping all the functionality separate enough to be reusable.eg; <?php arrayToDropDown(getCatigorys()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072151 Share on other sites More sharing options...
Alex Posted June 15, 2010 Share Posted June 15, 2010 Btw, the reason what you did wasn't working is because once you return something from a function it doesn't continue processing, that's the end of the function. So you can't return from a single function call multiple times. Edit: Oh, and also to address the indirect question a bit: What the person in the blog post you read was talking about was programming functions without side-effects. I'd suggest reading this article for more information: http://en.wikipedia.org/wiki/Side_effect_(computer_science) Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072160 Share on other sites More sharing options...
V Posted June 15, 2010 Author Share Posted June 15, 2010 @thorpe thanks you for the explanation I will try to do it that way @AlexWD, Ooh, I see. I should remember that thanks Quote Link to comment https://forums.phpfreaks.com/topic/204790-dont-use-echo-in-functions/#findComment-1072161 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.