views:

1859

answers:

8

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

Method 1 - variable concatenation

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

Method 2 - output buffering

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

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

Cheers!

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.

+2  A: 

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.

Darryl Hein
+1  A: 

It's true that there is a slight overhead in constantly modifying a variable and reprinting it. However, not doing that means that at some point the script may finish running (due to an interruption or similar), and the sequential echo statements will have partially printed the page rather than nothing.

warren
+1  A: 

I was going to type out a long reply about how PHP strings are mutable (opposed to immutable strings like in C or C#), but I think I'll just link to an older post I came across. I basically deals with what you're asking, in respect to the Java and C# solution of using a stringbuilder.

sidenote: the stringbuilder solution would be similar to (untested):

$html = array();
$html[] = '<ul>';
for ($k = 1; $k < = 1000; $k++){
    $html[] = '<li> This is list item #';
    $html[] = $k;
    $html[] = '</li>';
}
$html[] = '</ul>';
echo implode('',$html);
Erik van Brakel
+1  A: 

Just a couple of thoughts:

  • Output buffering can make you pages look slow, since the user sees nothing until the entire script has run (although the way you have #1 setup the same would hold true).

  • Strings in php are muteable, so concatenation is not nearly as bad as in some other languages. That being said, Output buffering might be just a tiny bit faster, as the space allocated for the input is fairly large by default (40K according to this)

In the end I'd say it's really more a question of style. If you like what output buffering buys you, then it is probably the way to go.

Paul Wicks
+1  A: 

Answers to your question can also be found here: http://stackoverflow.com/questions/111282/php-output-w-join-vs-output

String concatenation is the fastest way to build strings.

I haven't tested "echo" vs string building, but as long as you're not using buffered output echo should be the fastest due to sequential writes to a self-flushing buffer. ( only slowdown being in the flush, which you won't really avoid even if you do string-concatenation in advance )

Kent Fredric
+4  A: 

The idea of string concatenation itself aside, you're really asking (I think) how you should be building up web pages, and it strikes me that any form of explicit concatentation is probably the wrong thing to do.

Try using the Model-View-Control pattern to build up your data, and passing it to a simple templating library (like Smarty), and let it worry about how to build your view.

Better separation, fewer concerns.

GaryF
A: 

Something I haven't seen mentioned is that, usually, the PHP guy has to work with design people that need to class the HTML or add styles some other way, so the solution must bear that in mind.

Adriano Varoli Piazza
+1  A: 

It's a bit old, but this post by Sara Golemon will probably help. AFAIK the output buffering functions are quite fast and efficient and so is echo, so that's what I would use.

too much php