
In PHP, output buffering feature is used to control program output. Controlling output will be done with respect to time of sending output to the browser, the order of output and etc.
Normally, each chunk of data specified as PHP printing statements or as HTML content in between PHP script will be sent to the browser once it is ready. But, using PHP output buffering, these data chunks will be stored in a buffer.
For controlling PHP program output through buffers, we need to enable output buffering by using any one of the following two methods.
There are three configuration directives used for this output control. These are,
First, two directives can be set in any one of the files, php.ini, .htaccess, httpd.conf or user.ini. And, the last configuration directive of the above list will be set via anyway, including with ini_set() function.
By setting above directives with PHP configuration files, will affect all PHP script. But, enabling PHP output buffering on a need basis is preferable than this global configuration.
So, PHP includes PHP functions to control output buffering. These are listed below with their short description.
By using above primary functions of PHP output buffering, we are going to see an example program.
<?php ob_start(); print "PHP Output Buffering: level-1<br/>"; ob_end_flush(); ob_start(); print "PHP Output Buffering: level-2<br/>"; ob_flush(); print "Store into recently created buffer on level-2<br/>"; ob_end_flush(); ?>
In the above PHP program, we have created a buffer using ob_start() in the very first line. And then, the data specified with PHP print statement is stored into this buffer. And, another function ob_end_flush() is called to flush buffered data and to destroy buffer subsequently.
And then, again ob_start() is invoked to create fresh buffer filled with the print data. But, this time, PHP ob_flush is used instead of ob_end_flush(). So, it will only flush buffered data to user’s display, and not destroy buffer.
So, the third print statement will store data into the recently created buffer.
Apart from the above list of primary functions, PHP supports several functions in this extension. These are used to deal with the following list of functionalities.
Now, let us see all PHP functions by categorizing with respect to the above list of output buffering functionalities.
Using PHP output buffering function, we can get the details like, contents of the buffer, length of the buffered data and etc.
We can take the previous example to learn how to subsequent buffers into a stack. If we create buffers subsequently by invoking ob_start(), then, each newly created buffers will be pushed onto the stack on top of the previous output buffer. For example,
<?php ob_start(); print "PHP Output Buffering: level-1<br/>"; ob_start(); print "PHP Output Buffering: level-2<br/>"; ob_end_flush(); ?>
In PHP, buffered output data will be compressed by specifying ob_gzhandler as an argument of ob_start() function. ob_gzhandler() is an in-built PHP function, used as an callback. With the help of this callback, PHP returns compressed data to the browser.
Though it takes some time to compress output data, the time taken by the browser to download compressed data will be reduced.
There is two more PHP output buffering control functions while working with URL rewriting. These are,
Note:
Hi Vincy,
Im web developer at uOttawa
Nice to see women taking place in It world
Best regards!
Thank you Rosa. I am happy to know about you too :-)
hi vincy you are doing great Job!
Thank you Jaikishan. Keep reading and sharing.