Is it better to use ob_get_contents() or $text .= 'test';

此生再无相见时 提交于 2019-12-10 12:43:36

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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();



回答5:


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();


来源:https://stackoverflow.com/questions/292068/is-it-better-to-use-ob-get-contents-or-text-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!