title: 'Trying to store an HTML table as a PHP variable, but built with a foreach loop. Possible?
body: <p>Hi I think this is just a syntax problem, but I might be doing something PHP isn't designed to do.</p>
  
  <p>I'm trying to draw an HTML table, using an array to populate the table. The headers for example.</p>
  
  <p>Where $headers is an array of headers I have attempted:</p>
  
  <pre><code>$html_table = '
                  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;
                      &lt;tr&gt;
                          foreach($headers as $header)
                          {
                          echo "&lt;th&gt; $header &lt;/th&gt;";                          
                          }
                      &lt;/tr&gt;
  ';
  </code></pre>
  
  <p>The idea being to generate:</p>
  
  <pre><code> &lt;table border="1" cellspacing="0" cellpadding="2"&gt;
      &lt;tr&gt;
          &lt;th&gt;Heading 1&lt;/th&gt;
          &lt;th&gt;Heading 2&lt;/th&gt;
          &lt;th&gt;Heading 3&lt;/th&gt;
          ...
          &lt;th&gt;Heading 99&lt;/th&gt;
      &lt;/tr&gt;
  </code></pre>
  
  <p>Simply by later asking for 
      $html_table</p>
  
  <p>Suffice is to say at the moment I get a single headed column with "$header" in it, becuase the loop is not running inside the variable equation.</p>
  
  <p>The reason I'm storing the HTML as a variable like this is because I want to concatenate (usage?) it with other generated html, i.e.</p>
  
  <pre><code>$html_table .= '&lt;/table&gt;';
  </code></pre>
  
  <p>later on (of course the actual bit in the middle is more complicated, to do with retreiving data from databases to populate the table.</p>
  
  <p>Where am I going wrong? Thanks</p>