Back to Top

How To Turn On Output Buffering

From the PHP version 4, PHP supports output buffering.Output buffering allows you to have all output of PHP code stored into an internal buffer instead of immediately transmitted to the browser.

To Turn On Output Buffering:

To add following line of code before you start actually generating the page:

By default, Output buffering is in OFF mode. and above line of code turns on output buffering.All output is stored in an internal buffer. After than you can add or write the PHP code into a page.

NOTE: Always call ob_start( ) at the top of your page and ob_end_flush( ) at the bottom.

To take content from Output Buffer:

After all the content is generated, you take the content and flush it by adding the following line of code.

ob_get_contents() function returns the current contents of the output buffer as a string. You can then do whatever you want with it.

To Send Output Buffer to browser

Here,ob_flush() is used to discard the content of the buffer whereas ob_end_flush() flushes the entire buffer and turn off output buffering.

ob_flush() is used when you want to flush section of the page to the client, whereas ob_end_flush() flushes the entire buffer, then destroys the buffer.

NOTE: The output won’t be sent until ob_flush() or ob_end_flush( ) is called

ob_end_flush() stops buffering and sends the current contents of the buffer to the browser.

To Flush Output Buffer

If you just wanted to take the contents into a string and not want to send those contents into the browser, you could call ob_end_clean() to end output buffering and which destroy the contents of the buffer.

NOTE:It is important to note that both ob_end_flush() and ob_end_clean() destroy the buffer. In order to capture the buffer’s contents for later use, you need to make sure to call ob_get_contents() before you end buffering.

Example:

Let’s see example for Output buffering:

and You get below error:

Cannot add header information – headers already sent

All the headers must be sent at the beginning of the response because by default PHP sends out content as it comes and if you send headers after page text, you will get an error.With output buffering, the transmission of the response awaits a call to flush() and the headers are sent synchronously. so the following works fine:

ADVANTAGE

Basically, Output buffering can be used to improve network performance and implementing content compression.

Must Read:

Buffered Vs Unbuffered Queries in PHP
Include a file and store contents to variable using output buffer
How to use CONST keyword in PHP

I hope you have enjoyed this article. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (2)

  1. […] must know about Output Buffering in PHP.Last time ,my friend Bhumi has written an article about Output Buffering in PHP where she has showed you how to turn on and use Output Buffering in […]

  2. I’ve been browsing online greater than 3 hours as of late, but I by no means found any interesting article like yours. It is beautiful worth sufficient for me. In my view, if all website owners and bloggers made good content as you did, the net shall be a lot more helpful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How To use Store Procedure with PDO

Posted on 12 years ago

Bhumi

How to Create ShortCode in WordPress

Posted on 12 years ago

Bhumi

How to use Micro Data in HTML5

Posted on 9 years ago

Bhumi