PHP Output Buffering

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, 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 into a buffer.

PHP Output Buffering Configuration

For controlling PHP program output through buffers, we need to enable output buffering by using any one of the following two methods.

  • Setting PHP Output buffering configuration directives.
  • Enabling output buffering by PHP functions.

Setting PHP Output Buffering Configuration Directives

There are three configuration directives used for this output control. These are,

  • output_buffering – Contains 0 by default. We need to turn it on by giving “ON” as its value that makes to enable output buffering for all PHP scripts under root directory as a whole.
  • output_handler – It contains NULL as its default value. We need to specify PHP inbuilt function reference to this directive for sending output to that function.
  • implicit_flush – Turning this directive to “ON”, will make PHP script to flush output after each PHP printing statements and HTML block.

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 any way, including with ini_set() function.

php_output_buffering

Enabling Output Buffering Control by PHP Functions

By setting above directives with PHP configuration files, will affect all PHP script. But, enabling PHP output buffering on need basis is more preferable than this global configuration.

So, PHP includes PHP functions to control output buffering. These are listed below with their short description.

  • ob_start() – This creates new buffer into which each chunk of data is stored. These data chunks will be provided in a PHP script by using printing statements or in the form of HTML content.
  • ob_flush(), ob_end_flush() – These functions are used to flush output buffers to display output to the browser. These are differed where, ob_flush()is not destroying buffer after flush, and the other will do. Once ob_end_flush(), we need to recreate a fresh buffer again, if required.
  • ob_clean(), ob_end_clean() – These functions are used to clear buffered data. And, ob_end_clean() will destroy buffer after clearing its content.

PHP Output Buffering Example

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 destroying buffer. So, the third print statement will store data into recently created buffer.

Output Buffering Control Functions

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.

  • Reading data from output buffers.
  • stacking output buffers.
  • Compressing buffered data.
  • URL rewriting.

Now, let us see all PHP functions by categorizing with respect to the above list of output buffering functionalities.

Reading Data from Output Buffers

Using PHP output buffering function, we can get the details like, contents of the buffer, length of the buffered data and etc.

  • ob_get_contents() — This function returns output buffer content.
  • ob_get_length() — This function is used to get length of the buffer.
  • ob_get_level() — It is used to get level output buffer.
  • ob_get_status() — It is used to get array status information either for top level buffer or for all output buffer levels.
  • ob_get_flush() — This functions creates combinatorial effects of both ob_get_contents() and ob_end_flush().
  • ob_get_clean() — Similarly, this will be the combination of ob_get_contents() and ob_end_flush().

Stacking Output Buffers.

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 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();
?>

Compressing Buffered Data

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.

URL Rewriting.

There are two more PHP output buffering control functions, while working with URL rewriting. These are,

  • output_add_rewrite_var() — It accepts name and value pair as its argument. This name value pairs will be added as a query string of a URL. And, a new form hidden field will be added with this pair.
  • output_reset_rewrite_vars() — This function will clear all URL rewrite values.

Note:

  • PHP is capable of doing automatic output flush while reaching end of PHP delimiters, even if no output buffering functions are specified.
  • With PHP output buffering, it wont affect sending headers using PHP header() or setcookie(), after any PHP output statement.

This PHP code tutorial was published on September 10, 2013.

↑ Back to Top