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.
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.
We just need to call one PHP function to start the PHP Output Buffering. Have a look at below code block:
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:
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:
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:
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:
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:
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.
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().
To see this in action better if you create two different PHP file and check it executing those files.
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.
Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.
View all posts by: Avinash