colombian Posted May 13, 2008 Share Posted May 13, 2008 Here is my problem, I need this function to be part of a string of outputs. $output = "Html" //etc $output .= "select"; $output .= attending($total_people); However, it only returns 1 of the 9 supposed values for the select box. <?php function attending($var) { $hour = range(1,9); foreach ($hour as $h) { if($var == $h) { $selected = "selected"; } else { $selected = ""; } return "<option {$selected} value=\"{$h}\"> {$h} </option>"; } } ?> If I use "echo" instead of "return" it does give me all 9 outputs, but then I cannot make the form dynimically through the $output variable. Any help is appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/105497-solved-foreach-in-function-only-returns-1-value/ Share on other sites More sharing options...
wildteen88 Posted May 13, 2008 Share Posted May 13, 2008 chnage return "<option {$selected} value=\"{$h}\"> {$h} </option>"; } to $html .= "<option {$selected} value=\"{$h}\"> {$h} </option>"; } return $html; Quote Link to comment https://forums.phpfreaks.com/topic/105497-solved-foreach-in-function-only-returns-1-value/#findComment-540359 Share on other sites More sharing options...
colombian Posted May 13, 2008 Author Share Posted May 13, 2008 Thank you! That did the trick - quick question though. Why does doing $html = (no .= ) cause it to break? More over, why does declaring $html before that loop cause it to break too? I had to declare $html up with the $hour array, and leave as is to make PHP happy. Just trying to understand what PHP is doing here. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/105497-solved-foreach-in-function-only-returns-1-value/#findComment-540387 Share on other sites More sharing options...
DarkWater Posted May 13, 2008 Share Posted May 13, 2008 The "return" language construct immediately stops execution of the function, so you need to append the whole HTML segment into a variable and return the whole thing. Quote Link to comment https://forums.phpfreaks.com/topic/105497-solved-foreach-in-function-only-returns-1-value/#findComment-540388 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.