ttmt_101 Posted July 11, 2013 Share Posted July 11, 2013 Hi all I want to create a simple html select menu from an array like: <select> <option value="">Red</option> <option value="">Green</option> <option value="">Blue</option> </select> I know I can output this like: $my_Arr = array('Red','Green','Blue'); echo "<select>"; for($i=0; $i<count($my_Arr); $i++){ echo"<option>".$my_Arr[$i]."</option>"; } echo "</select>"; But I would like to add the select menu to a variable and use later. So I can start the variable off like: $select_menu = "<select>"; But how can I add the rest of the html code to the variable as it's created, so I end up with: $select_menu = "<select> <option value="">Red</option> <option value="">Green</option> <option value="">Blue</option> </select>" Quote Link to comment https://forums.phpfreaks.com/topic/280058-add-html-code-to-php-variable/ Share on other sites More sharing options...
Solution requinix Posted July 11, 2013 Solution Share Posted July 11, 2013 Append to the string, like $select_menu = "<select>"; $select_menu = $select_menu . "<option value=''>Red</option>"; $select_menu .= "<option value=''>Green</option>"; // .= is shorthand for the above $select_menu .= "<option value=''>Blue</option>"; $select_menu .= "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/280058-add-html-code-to-php-variable/#findComment-1440280 Share on other sites More sharing options...
ttmt_101 Posted July 11, 2013 Author Share Posted July 11, 2013 Thanks Still alive I've been doing '+=' for the last 30 mins instead of '.=' Quote Link to comment https://forums.phpfreaks.com/topic/280058-add-html-code-to-php-variable/#findComment-1440281 Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 There is also a very easy-to-use foreach construction. Quote Link to comment https://forums.phpfreaks.com/topic/280058-add-html-code-to-php-variable/#findComment-1440282 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.