PHP Output Buffering

Output buffering is a very Good feature of PHP. You can have control over your output using the output buffering. You can decide what to buffer and when to output to the browser.

What is Output Buffering?

When you execute any PHP script and generate an HTML content, that HTML content is server to the browser. But what if you want to generate and HTML content and don’t pass it to browser? Yes, here Output Buffering comes into picture.

Normally PHP don’t do it, but when you start Output Buffering PHP will keep all your output content in Internal Buffer rather than passing it to browser. PHP will pass this content only when user has requested to output the buffer content or script execution ends.

Default state for Output buffering is OFF.

How to Start Output Buffering?

We just need to call one PHP function to start the PHP Output Buffering. Have a look at below code block:

ob_start();
  ob_start();

How to send output buffer to browser?

Once output buffer is started nothing will be outputed to the browser untill you request to output or script execution ends. If you want to output all the content of the buffer to browser then you need to use below code block:

ob_flush();
  ob_flush();

But the thing is that, ob_flush(); will output the content from the buffer but PHP script will continue to buffer further. So what if you want to output the buffered content and stops the buffering anymore? You will need below code block for the same:

ob_end_flush();
  ob_end_flush();

How to delete Output Buffer?

Just like an outputing, deleting is also done by just one function call. If you want to delete all content from the buffer then you will need below code block:

ob_clean();
  ob_clean();

But like ob_flush(), ob_clean(); will continue to buffer the content, So to delete the content and stops the buffering anymore you need to use below code:

ob_end_clean();
  ob_end_clean();

How to get Output Buffer Content?

So now we have seen the functions for deleting and outputing the buffer values, but what If you just want to check the content which are stored in the PHP buffer? Simple you will need to use below code block for the same:

$var = ob_get_content();
  $var = ob_get_content();

Working Example

Have a look at below code block. Here you can see that we are outputting the phpinfo() and then after we have used sleep(10). We have not used ob_start() here. So what happes, it will output the phpinfo and the it will wait for sleep to finish.

phpinfo(); sleep(10);
  phpinfo();
  sleep(10);

Now in below code block you can see the magic of the output buffering, here you can see that we have used ob_start() and sleep() together. Execute below code in any php file and you can see that PHP script will wait for 10 seconds of sleep even if that is called after phpinfo().

ob_start(); phpinfo(); sleep(10);
  ob_start();
  phpinfo();
  sleep(10);

To see this in action better if you create two different PHP file and check it executing those files.

Benefits

One major benefit for Output beffering is caching. Yes you can cache the content (in fact whole rendered page) with the help of output beffering. Just to explain how this works. You can start output buffering and you can buffer whole content and at the end of the script before echoing the output you will have content of whole page, which you can cache in any ways you want.

Share This Article

Tags: