Ayoade David
aydavid

Follow

aydavid

Follow
OUTPUT BUFFERING IN PHP - Usage & Functions

OUTPUT BUFFERING IN PHP - Usage & Functions

Ayoade David's photo
Ayoade David
·Dec 6, 2022·

2 min read

Perhaps you have come across PHP functions like ob_flush, ob_get_clean, ob_get_contents etc previously in php code snippets, these are output buffering functions that are very important for backend development, they allow us to store the buffer (text or html) in a php script into a variable for later use. A basic usage captures any following output (html or text) with ob_start, collect the captured content with ob_get_contents into a variable, end the process with ob_end_flush

<?php ob_start();    //capturing ?>
This is my html
<?php 
           $thehtml = ob_get_clean(); //collect and clean
           print_r($thehtml); //usage
           ob_end_flush();//end
?>

other methods included are :

ob_start() Starts the output buffer, capture 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() Triggers both ob_get_contents() and ob_end_clean()

ob_flush() Flush the content buffer and send it to the browser without ending the buffer

ob_implicit_flush() Enables implicit flushing after every output call.

ob_end_flush() Flush the content buffer and send it to the browser also ending the buffer

ob_get_level() Returns the current nesting level of the output buffer

TYPICAL USAGES OF OUTPUT BUFFERS

Remove line breaks or whitespaces from html output.

For example, in the code below, i want to remove the line-breaks in my php output (html) to make it harder to read for a bug or venurability hunter using view-source or dev-tools

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

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

my output becomes

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

Print HTML multiple types to output

<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>

Write HTML into file.

<?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 simply stores text and html output for later use, they are very useful in manipulating output and as a better way of rendering output. They are also used in navigating the popular php header already sent error.

 
Share this