Output Buffering In PHP - Usage & Functions

Output Buffering In PHP - Usage & Functions

You may have encountered PHP functions like ob_flush, ob_get_clean, ob_get_contents, and more in PHP code snippets. These are output buffering functions that hold significant importance in backend development. They allow us to capture the output (text or HTML) in a PHP script and store it in a variable for later use. Basic usage involves capturing output with ob_start, collecting the captured content with ob_get_contents into a variable, and ending the process with ob_end_flush.

<?php
ob_start();    // Start capturing
?>
This is my HTML
<?php 
$theHTML = ob_get_clean(); // Collect and clean
print_r($theHTML); // Usage
ob_end_flush(); // End
?>

Other relevant methods for output buffering include:

  • ob_start(): Starts the output buffer and captures all contents.

  • ob_get_contents(): Returns all content captured by ob_start().

  • ob_end_clean(): Empties the output buffer and turns it off for the current nesting level.

  • ob_get_clean(): Combines both ob_get_contents() and ob_end_clean().

  • ob_flush(): Flushes the content buffer and sends it to the browser without ending the buffer.

  • ob_implicit_flush(): Enables implicit flushing after every output call.

  • ob_end_flush(): Flushes the content buffer and sends it to the browser, ultimately ending the buffer.

  • ob_get_level(): Returns the current nesting level of the output buffer.

TYPICAL USAGES OF OUTPUT BUFFERS

Removing Line Breaks or Whitespaces from HTML Output

Consider the following code example where we want to eliminate line breaks in PHP output (HTML) to make it less readable for anyone inspecting the source code using view-source or developer tools.

<?php ?>
<html>
    <title></title>
    <body></body>
</html>

Using output buffering:

<?php ob_start(); ?>
<html>
    <title></title>
    <body></body>
</html>
<?php

function minifySpaces($buffer){
    return str_replace("\n", " ", $buffer);
}

$buffer = ob_get_clean();
print_r(minifySpaces($buffer));
ob_end_flush(); 
?>

With this approach, the output becomes:

<html><title></title><body></body></html>

Printing HTML Multiple Times to Output

You can also print HTML multiple times to the output, as demonstrated below:

<html>
    <title></title>
    <body>
        <?php ob_start(); ?>
        <a>LINK</a>
        <?php
        $buffer = ob_get_clean();
        print_r(($buffer));
        print_r(($buffer));
        print_r(($buffer));
        ob_end_flush(); 
        ?>
    </body>
</html>

Writing HTML into a File

To write HTML into a file, you can utilize output buffering as shown in this example:

<?php ob_start(); ?>
<html>
    <title></title>
    <body></body>
</html>
<?php

function minifySpaces($buffer){
    return str_replace("\n", " ", $buffer);
}

$buffer = ob_get_clean();
file_put_contents("output.html", $buffer);
ob_end_flush(); 
?>

Output buffering is a valuable technique for storing text and HTML output for later use. It is particularly useful for manipulating output and improving the rendering process. Additionally, output buffering can help in handling the common PHP "header already sent" error.