PHP Output Buffering

by Vincy. Last modified on July 9th, 2022.

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.

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 the 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 the PHP script flush output after each PHP printing statement 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 in any way, including with ini_set() function.

Enabling Output Buffering Control by PHP Functions

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.

  • ob_start() – This creates a 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 differ where ob_flush()is not destroying buffer after the 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 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.

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 them with respect to the above list of output buffering functionalities.

Reading Data from Output Buffers

Using the PHP output buffering function, we can get the details like the 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 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();
?>

Compressing Buffered Data

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.

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. These name-value pairs will be added as a query string of an 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 the end of PHP delimiters, even if no output buffering functions are specified.
  • With PHP output buffering, it won’t affect sending headers using PHP header() or setcookie(), after any PHP output statement.

Comments to “PHP Output Buffering”

Leave a Reply to Vincy Cancel reply

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

↑ Back to Top

Share this page