views:

830

answers:

5

I have seen a lot of ob_get_clean() the last while. Typically I have done $test .= 'test'

I'm wondering if one is faster and/or better than the other.

Here is the code using ob_get_clean():

ob_start();

foreach($items as $item) {
    echo '<div>' . $item . '</div>';
}

$test = ob_get_clean();

Here is the code using $test .= 'test':

$test = '';

foreach($items as $item) {
    $test .= '<div>' . $item . '</div>';
}

Which is better?

+5  A: 

The results are the same, and I'd imagine the performance differences are negligible if any. Basically, a matter of personal style preference. I would go with concatenation myself - I use output buffering only when concatenation is not an option.

Also, instead of running both ob_get_contents() and ob_clean() simply run ob_get_clean() which performs both at once.

Eran Galperin
good call on the ob_get_contents vs ob_get_clean. changed
Darryl Hein
+4  A: 

Output buffers have all the pitfalls of global variables. You have to be aware of all execution paths from the ob_start() to the ob_get_clean(). Are you sure it will get there, and that any buffers opened in between will have been closed? Keep in mind that code can throw exceptions. That can be a really fun bug for the next guy to track down.

On the other hand--and I hate to even mention it--at one time output buffering was somewhat faster at concatenating large strings, for reasons internal to PHP. I'm not sure if that is still true.

Preston
+1  A: 

I think using output buffering may have a small performance benefit when you are using massive strings, but for common use you are better with concatenation in my opinion as this code will probably be easier to understand and debug by others.

A small point, but if you are going to use the output buffering approach, you may as well use it fully:

ob_start();

foreach($items as $item) {
    echo '<div>';
    echo $item;
    echo '</div>';
}

$test = ob_get_clean();
Tom Haigh
How big is "massive"?
Darryl Hein
hmm, yeah, dunno. i haven't done any measurements. fair point
Tom Haigh
A: 

As mentioned above, output buffering is better for performance. For large strings, the performance difference isn't neglible at all. You can compare output buffering in PHP to StringBuffer/StringBuilder in Java; for string concatenation, the whole string needs to be copied in memory every time. For output buffering, the text goes into a buffer which grows in sensible incremements, and there is no need to copy the data for each output.

You might also think about using inline HTML (even faster). Added benefit for inline HTML is that your IDE can likely perform syntax highlighting on the HTML, which won't happen when the HTML is inside a PHP string. Modified code (short_open_tags = On required):

ob_start();
?>

<? foreach($items as $item) { ?>
    <div><?= $item ?></div>
<? } ?>

<?
$test = ob_get_clean();
Alexander Malfait
A: 

If you're concerned about the overhead of string concatenation you should be note that this:

echo '<div>'.$test.'</div>';

is measurably slower than this:

echo '<div>', $test , '</div>';

The first compiles down to two string concats followed by an echo, while the second compiles down to just three echoes, which is actually quicker.

Ciaran McNulty