In PHP, the output buffering feature is used to control program output. Controlling output will be done with respect to the 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 scripts 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 in any way, including with ini_set() function.
By setting the above directives with PHP configuration files, will affect all PHP scripts. But, enabling PHP output buffering on a need basis is preferable to this global configuration.
So, PHP includes PHP functions to control output buffering. These are listed below with their short description.
By using the 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 the PHP print statement is stored in this buffer. And, another function ob_end_flush() is called to flush buffered data and destroy the buffer subsequently.
And then, again ob_start() is invoked to create a 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 the user’s display, and not destroy the buffer.
So, the third print statement will store data in 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 them with respect to the above list of output buffering functionalities.
Using the PHP output buffering function, we can get the details like the 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 buffer 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 the 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 are 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.