views:

72

answers:

4

OK, 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? Don'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/advantagesof using ob_start() without any parameters set (like in the snippet above).

+2  A: 

If you have a PHP script that uses echo to output in lots of little pieces, it turns out the PHP script runs faster if you buffer with ob_start() and then flush it all when you're done.

(note: this is by report only from blogs by PHP core developers; I haven't test it myself so I don't have specific numbers for how much faster it is.)

Yes, it keeps the buffer in memory but how large is your HTML output? Likely pretty small compared to the memory limit of your PHP environment.

Yes, it delays output until it's all done, but since the PHP runs faster, there will be lower total elapsed time. The sooner your Apache thread can move on to another request, the better for your site's scalability.

This is not to say that every page benefits from using output buffering. The benefit may be insignificant in the typical PHP script that's mostly static HTML with a few variable elements.

Bill Karwin
A: 
  1. making sure you don't get a 'headers already sent' error
  2. catch all html data to create a cache file

I would only use it for 2 since I think 1 should never happen anyway

MrSoundless
A: 

One usage would be to prevent php sending headers until the script is ready to send its content, as this would allow you to alter your headers as you build your output.

Read this is a article about ouput buffering to get more ideas of how to use it. http://www.devshed.com/c/a/PHP/Output-Buffering-With-PHP/

Ivar Bonsaksen
+2  A: 

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

ob_start();
include 'file.php';
$output = ob_get_clean();

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 of phpinfo() with an output buffer, modify it, and then display it where you want in your template file.

ob_start();
phpinfo();
$info = ob_get_clean();
//Massage the output, remove the doctype, html, head, and body tags
echo $info;

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...

ircmaxell