ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Having fun with PHP’s output buffer

(Page 1 out of 3)


In this article you will learn what output buffering is, how to enable it, and how to perform some neat tricks with it.


I'm sure most of us have come across the dreaded "Cannot modify header information - headers already sent", and this is usually caused when your script tries to send a header or set a cookie, when it's already sent output. The easiest fix is to make sure no output has been sent, but it's also possible to enable something called output buffering which also solves the problem

Output buffering will not send any output to the visitor, before the script has completed. This means you can freely send output and use the setcookie() or header() function without having to worry about the correct order. But output buffering can do a lot more than that.

In this article I'll first show you how output buffering works, and take you through most of its basic functions. After that we'll have a look at what's possible with output buffering, and show you some neat tricks. Let's get started.

The basics

To enable output buffering in your scripts, all you need to do is call the ob_start() function at the top of your script. After that, you can print as much text or HTML as you want, without having to worry about headers and such. See the example below:

// Enable output buffering

// Print some output
echo 'Hello World';

// Set a cookie (normally this would produce an error)
setcookie('test', 'test');

// Print some more output
echo 'Hi There';


(View Live Demo)

With the output buffer on, no output will be sent to the browser until the script ends. But this might not be what you want, especially not when you've got a long-running script. This is no problem however, because it's possible to "flush" the output buffer yourself, with the ob_flush() function. When this function is called, all the output that has been collected by the output buffer will be sent to the client. This also means you won't be able to send headers after using the ob_flush() function, since output has now been sent to the client. The example below demonstrates the ob_flush() function.

// Enable output buffering

// Print some output
echo 'Hello World';

// Flush output buffer (thus sends 'Hello World' to the client)

// Print some more output
echo 'Hi There';


(View Live Demo)

You probably won't really see a difference in both examples, but for a much more complicated script, especially when using HTML tables, you will notice a difference.

It's also possible to discard everything that's been collected by the output buffer, with the ob_clean() function. This function will completely empty the output buffer. See the below example.

// Enable output buffering

// Print some output
echo 'Hello World';

// Empty output buffer

// Print some more output
echo 'Hi There';


(View Live Demo)

In the above example 'Hello World' will never be seen by the client, since it's been discarded.

If you decide half-way through a script that output buffering needs to end, you can use two functions for this: ob_end_clean() and ob_end_flush. The ob_end_clean() function will end the output buffer, and discard anything that has been collected by it. The ob_end_flush() function will also end the buffer, but will first send any output that's been collected by the output buffer. Most of the times you'll probably want to use the ob_end_flush() function.

A really great aspect of output buffering is that it's possible to nest output buffers, meaning you can have multiple levels of output buffering. This might seem a bit complicated, so let's use a simple example to demonstrate this:

// Start buffer-1

echo 'Hello from buffer-1';

// Start buffer-2

echo 'Hello from buffer-2';

// Empty and end buffer-2

echo ' and hello from buffer-1 again';


(View Live Demo)

In the above example, the second buffer will never be sent to the client, and the first buffer will. As you can see, the second buffer was 'nested' into the first buffer.

When starting a new output buffer, it's possible to set a callback to be used when the output buffer is sent. This means you can modify any output just before it's sent. See the example below:

function my_callback($content) {
        $content .= ' Dennis Pallett';

        return $content;

// Start output buffer with callback

echo 'Hello';


(View Live Demo)

In the above example, we use an output callback to add my name to the end of the content. This immediately brings us to the next part of this article: creating an output "replace function".

Next: Output replace function »

7 Responses to “Having fun with PHP’s output buffer”

  1. Piku’s PHP Blog » PHPit: Having fun with PHP’s output buffer Says:

    […] In this article Dennis Pallett explains what the output buffer is, how to use it, and shares some neat tricks possible with the output buffer. Samples and demo code included. […]

  2. Sam’s random musings » Having fun with PHP’s output buffer Says:

    […] One of the more powerful and handy features that PHP offers is output buffering. It allows you more control over when the client’s browser gets the information instead of just spewing information at random. It is a little tricky to get the hang of, so has put together this new tutorial on how to get started. […]

  3. sam Says:

    Hi i’ve heard that its possible to output to the browser while the script is still running(like search query) using some buffer and/or flush function.if u can write or direct me towards such links it would be immensly helpful

  4. Andy W Says:

    Excellent this really helped me understand, the headers all ready sent error is a nightmare when coding in PHP by understanding and using output buffering all my scripts redirect as they should.

    Thanks guys.

  5. MA Razzaque Rupom Says:

    Output buffer compression to save bandwidth is a good option. Nice to see this type of writting in this site.

  6. PHPit - Totally PHP » Create your own HTML widgets with PHP Says:

    […] But with a little help from PHP, we can still create our own HTML tags, and have it working in all browsers. In this tutorial I will show you how. First we’ll go through the basics of creating a new tag, and after that we’ll explore some more advanced functionality. We’ll be making use of an output buffer to create our own tags, so if you’ve never used the output buffer before, it’s probably useful to read the following article first: Having fun with PHP’s output buffer. Let’s get started! […]

  7. Brian Cray Says:

    I have written a few articles about advanced PHP output buffering. Check them out at

    If you look around my web site, you will find more articles about things to do with it.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & The Basics
  2. Output replace function
  3. Own HTML tags & GZip compression
Bookmark Article
Download Article
Download this article as a PDF file