Backend Development

What is Output Buffering in PHP

What is Output Buffering in PHP

PHP has an interesting output control technique called Output Buffering, in which the script can control sending output to the client or not. In this blog post we will talk about output buffering.

 

 

 

Output Buffering allows the script to stop printing output to the client. Instead the output is buffered in an internal memory. Once the buffer memory got filled the script flush the output to the client or browser.

 

Why Use Output Buffering?

  • Modify output before sending (e.g. compress HTML, add headers).
  • Avoid ‘Errors Already Sent’ errors.
  • Capture output from functions or template for later use.
  • Create dynamic caching systems.

 

Here are the primary functions used in PHP output buffering:

  • ob_start()
  • ob_end_flush()
  • ob_get_flush()
  • ob_flush()
  • ob_clean()
  • ob_get_clean()
  • ob_end_clean()

By default Output buffering is disabled on most php environments. But it can be turned on by calling ob_start() function at the top of the script, or by setting output_buffering and output_handler php.ini settings.

php.ini

output_buffering=On

This will turn on Output Buffering globally for all PHP applications on the server.

To turn on Output Buffering only for the current script, this is done by calling ob_start() at the top of the script:

<?php
ob_start();

 

To turn of the active buffer you can call ob_get_flush(), ob_end_clean(), ob_get_clean() or ob_end_flush()

To flush the active buffer, you can call ob_get_flush(), ob_end_flush(), ob_flush(). Flushing means sending discarding the contents of the active buffer.

Active buffer also get flushed when the size of the output exceeds the size of the buffer.

The contents of the active buffer can be retrieved by calling ob_get_contents(), ob_get_clean(), or ob_get_flush(). We will see an example below of how to include a file and get it’s contents in a variable.

 

Example:

<?php
ob_start();

echo "Hello World";
print("Hello World");

ob_get_clean();

When running this script, you will see no output just because php when it encounters ob_start() and ob_get_clean() it starts the output buffering and no further output get printed. 

To get the output of the buffer, let’s call the ob_get_contents() before ob_get_clean():

<?php
ob_start();

echo "Hello World\n";
print("Hello World");

$output = ob_get_contents();

ob_get_clean();

echo $output;    // Contain the buffer content

The ob_get_contents() function store the contents of the active buffer. As you see i stored the contents in the $output variable and printed it below. Running the script again will print:

Hello World 
Hello World

You can also use ob_clean() function in place of ob_get_clean(). The difference is that ob_clean() cleans the contents of active buffer without turning it off, while the ob_get_clean() cleans the contents of active buffer and turn it off.

 

Example: Multiple output buffers in the same script:

<?php
ob_start();

echo "Hello World\n";
print("Hello World");

$output = ob_get_contents();

ob_end_clean();

echo $output;


ob_start();
echo "\nSecond Line\n";

$output = ob_get_contents();

ob_get_clean();

echo $output;

ob_start();
echo "\nThird Line\n";

$output = ob_get_contents();

ob_get_clean();

echo $output;

As shown in this example ob_start() is called multiple times along with ob_get_clean(). This creates multiple buffers in the same script and you can control whatever output you want to print.

 

Real world example: rendering a template

<?php

function renderTemplate()
{
    ob_start();

    include "header.html";
    include "body.html";
    include "footer.html";

    return ob_get_clean();
}

echo renderTemplate();

You may saw a similar examples before like this. To include a file and store it’s contents in a variable. This is what we done in the renderTemplate() function which renders an html template by including the template files.

Note that while output buffer is active with ob_start() no output is printed from the include statement.

To get the real output you can call the ob_get_contents() or ob_get_clean() functions. The ob_get_clean() function works the same like ob_get_contents() but in addition it turn off the output buffer after returning the data.

 

Example: ob_start() with a callback:

<?php

ob_start(function($buffer) {
    return (str_replace("square", "circle", $buffer));
});

echo "Earth is square";

ob_end_flush();

The ob_start() function can accept a callback which is null by default. The callback triggered when the output buffer is flushed (sent), cleaned. The callback accepts the current buffer content.

You may use this callback to apply transformations on the data before being output to the browser as you see in this example.

 

0 0 votes
Article Rating

What's your reaction?

Excited
0
Happy
0
Not Sure
0
Confused
0

You may also like

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments