3

I'm writing a wordpress widget plugin. It should output some html code which should look like this

<a href="link1">link1title</a>
<a href="link2">link2title</a>
<a href="link3">link3title</a>
<a href="link4">link4title</a>
<a href="link5">link5title</a>

I'm running a for loop to output the links and titles from 2 arrays, and I can do that in two different ways:

<?php for ($i = 0; $i < $x; $i++)
    echo '<a href="'.$links[$i].'">'.$titles[$i].'</a>';
?>

Or I can use something like this:

<?php ob_start();
for ($i = 0; $i < $x; $i++) {?>
    <a href="<?php echo $links[$i];?>"><?php echo $titles[$i];?></a>
<?php ob_get_flush();?>

The example is trivial. There is a lot more html code, and a lot more variables involved. Since there is a speed difference between printf and cout in c/c++, I was wondering is there a speed difference between using output buffer and echo.

Also, which one is better to use?

Sibin Grasic
  • 714
  • 9
  • 27

1 Answers1

3

Of course there is. With echo, you're dumping to the output stream. With ob_start, you are creating a buffer which must then be processed (the optional argument to ob_start before being dumped to the output stream.

Since the second one is basically "do some stuff, then do the exact same as the first one", it is obvious that it will be slower.

However, if used correctly then the benefits can far outweigh the drawbacks.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 2
    `ob_start` is intended to be used with a callback to do post-processing on the content before sending it to the server. It has the side-effect that you can use the `header` function at any time if `ob_start` is called before sending anything, but aside from that calling it without purpose is useless. – Niet the Dark Absol Jul 06 '12 at 19:47
  • You wildly overlook the major drawback of outputting content through the network to the client each time you call echo, don't you? unless I don't understand what the point of `ob_start` is anyway, I thought it had something to do (along with many other things) with preventing the server to have to pass content through internet each time echo is called... http://stackoverflow.com/a/2832179/576767 – Félix Adriyel Gagnon-Grenier Nov 05 '14 at 15:43
  • @FélixGagnon-Grenier Why would that be a problem? The server still has to pass content along that stream, and most servers use internal buffering anyway. – Niet the Dark Absol Nov 05 '14 at 15:46
  • imagine there's a slow network on the client side. Having to pass 1200 echoes instead of one will slow your page load by 1200 times the little slowness of the network, won't it? – Félix Adriyel Gagnon-Grenier Nov 05 '14 at 15:48
  • Erm... no? In fact if anything it will make the page *seem* to load faster because the stuff at the top will load as soon as it is ready, rather than waiting for the whole thing. – Niet the Dark Absol Nov 05 '14 at 15:50
  • I don't understand how you consider that 1200 answers from a server (having to pass through internet, switches, firewalls, routers) can be as fast as one answer (having to pass only **once** through all this physical material) – Félix Adriyel Gagnon-Grenier Nov 05 '14 at 15:54