Jump to content

Add html code to php variable


ttmt_101

Recommended Posts

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>"
Link to comment
https://forums.phpfreaks.com/topic/280058-add-html-code-to-php-variable/
Share on other sites

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>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.