I can see the use of ob_start with the output_callback parameter set but I can’t see the use of ob_start when calling it without any parameters set at all.
Whats the point of disabling output to later throw all the output at once? Doesn’t this use more memory (server side) and slow downloads (client side) since the download starts only after the page is fully rendered (or when ob_end_flush is called)?
ob_start();
for ($i = 1; $i <= 15; $i++)
{
echo $i, ' ';
sleep(1);
}
ob_end_flush();
Anyone can give me the usage/advantages of using ob_start() without any parameters set (like in the snippet above).
One reason, is to “grab” the output of a small section of code.
So, let’s say that you have an independent piece of code that you want to execute, but you don’t want to just output it directly. What you can do, is
I’ll give you a real world example. Say you are building an installer for an application. And as part of that installer you want to show the current PHP information (the data from
phpinfo()). But, you want to integrate that information with the rest of the page (rather than use a frame). So, what you can do is grab the output ofphpinfo()with an output buffer, modify it, and then display it where you want in your template file.I also use it with view files. In the View class, the
__toString()method actually renders the view. But since__toString()is expected to return a string rather than echo it, I use output buffering to capture the template and return it…