Output Buffering

Stores the output of the page into a buffer, before being instructed to flush it's content to screen.


HTML is generally output directly as the page is processed in the order data appears on the page. However, this can be slow due to many smaller chunks of data having to be processed and their order being dictated within the page.


Output buffering overcomes these issues by processing the whole page, storing it in a buffer and then sending it when instructed.



To begin an output buffering use the ob_start() function, preferably as early as possible.


The page is then processed and all data is sent to an output buffer in memory.


At the end of the script use the ob_end_flush() function to send the data accumulated in the output buffer to the browser (this is also automatically carried out if not otherwise stated).


The ob_end_clean() function can be used to clean the output buffer without sending the data.


Both ob_end_flush and ob_end_clean turn off output buffering when called.


Using ob_start() to store the data into the output buffer and ob_end_flush() to deliver its content:


	echo "Here's some initial text that is only sent to the browser" ;

	ob_end_flush() ;

Simply ending the script has the same effect:


	echo "This has the same effect without the flush" ;



Using the ob_end_clean() function loses the data in the output buffer:


	echo "This text won't be displayed, since the output buffer is discarded using ob_end_clean() below." ;

	ob_end_clean() ;

Leave a Reply