<p>A question about different methods of outputting html from PHP; what are the performance differences between these:</p>

<p>Method 1 - variable concatenation</p>

<pre><code>$html = '';
$html .= '&lt;ul&gt;';
for ($k = 1; $k &lt; = 1000; $k++){
    $html .= '&lt;li&gt; This is list item #'.$k.'&lt;/li&gt;';
}
$html .= '&lt;/ul&gt;';
echo $html;
</code></pre>

<p>Method 2 - output buffering</p>

<pre><code>ob_start();
echo '&lt;ul&gt;';
for ($k = 1; $k &lt; = 1000; $k++){
    echo '&lt;li&gt; This is list item #',$k,'&lt;/li&gt;';
}
echo '&lt;/ul&gt;';
</code></pre>

<p>I suspect you get some performance hit from continually modifying and enlarging a variable; is that correct?</p>

<p>Cheers!</p>

<p>Thanks GaryF, but I don't want an answer about architecture - this question is about performance. There seem to be some different opinions / testing about which one is faster, which is why there is not an accepted answer as yet.</p>

I typically use method #1 so I can put that HTML any where in a template file that contains static HTML. I try to keep as much HMTL out of my PHP. It makes for much cleaner and shorter PHP, as the template is usually a separate file.
Regarding speed/performance, I'm thinking the difference will be very minor. With output buffering, it's also enlarging a variable all the time, although that variable isn't accessible, but it has to be stored somewhere.
I have often wondered if open and closing
```
<?php
```
is slower than just putting it all inside
```
<?php
```
and then echo'ing all at once.
I think in the end we are talking milliseconds in extremely complex scripts.
Oh yeah, method #1 is a lot more flexible as you can
```
echo $html;
```
anywhere.