Output Buffering for Web Developers, a Beginner’s Guide

Sunday, January 25th, 2009 Tags:

If you're not using PHP's output buffering, you should be. And if you are, you may not be using it to its potential.

In this article written specifically for web developers, I'll make a case for output buffering and show you how to get started within seconds. This article is the beginning of a series in which I'll share with you the awesome potential of output buffering.

Simple explanation of output buffering for Web developers

Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. Can you already begin to see the performance advantages and post processing opportunities?

Advantages of output buffering for Web developers

  • Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it's not being sent to the browser in pieces as PHP processes the HTML.
  • All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable.
  • If you've ever encountered the message "Warning: Cannot modify header information - headers already sent by (output)" while setting cookies, you'll be happy to know that output buffering is your answer.

Here's a "hello world" of PHP output buffering

<?php
// start output buffering at the top of our script with this simple command
ob_start();
?>

<html>
<body>
<p>Hello world!</p>
</body>
</html>

<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();
?>

It's that simple! Just by doing this our webpages appear less choppy as they render. Now let's take it one step futher.

The next step: compress the output

In the code below, ob_start() is changed to ob_start('ob_gzhandler'). That one simple change compresses our HTML, resulting in a smaller HTML download size for most browsers.

<?php
// start output buffering at the top of our script with this simple command
// we've added "ob_gzhandler" as a parameter of ob_start
ob_start('ob_gzhandler');
?>

<html>
<body>
<p>Hello world!</p>
</body>
</html>

<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();
?>

One more step further: custom post processing

In the code below, ob_start() is changed to ob_start('ob_postprocess'). ob_postprocess() is a function we define below used to make changes to the HTML before it is sent to the browser. Instead of Hello world!, the user will see Aloha world!

<?php
// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
ob_start('ob_postprocess');
?>

<html>
<body>
<p>Hello world!</p>
</body>
</html>

<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();

// ob_postprocess is our custom post processing function
function ob_postprocess($buffer)
{
	// do a fun quick change to our HTML before it is sent to the browser
	$buffer = str_replace('Hello', 'Aloha', $buffer);
	// "return $buffer;" will send what is in $buffer to the browser, which includes our changes
	return $buffer;
}
?>

Bonus tip: you can set cookies at any time with output buffering on

Since HTML is not sent directly to the browser, we can set cookies anywhere in our scripts without worrying about anything. Just turn it on like we did in step 1 and voilà! No more cookie problems.

Finished for now, subscribe for more

There you have it! Three simple steps into output buffering to get you started. First we just turned it on with ob_start(), then we added automatic compression with ob_start('ob_gzhandler'), and finally we made ob_postprocess()—a custom function to modify our HTML before output.

In following articles in this series, we'll build on this tutorial with specific examples that really take advantage of some massively cool opportunities with output buffering that you can plug right into your own scripts. Stay tuned!

Tags:

If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Brian Cray is a marketing & Internet strategy consultant, web developer, and Apple fanatic living in Columbus, Ohio, USA. His work has been featured by Mashable, NETTUTS, Smashing Magazine, and many others. He's happy to share his knowledge with you on DevTips and his own blog at Brian Cray.com.
  1. January 25, 2009 at 3:49 pm
  2. January 25, 2009 at 3:51 pm
  3. January 25, 2009 at 7:27 pm
  4. January 25, 2009 at 7:39 pm
  5. Alex Hardy
    January 26, 2009 at 7:42 am
    • January 26, 2009 at 9:53 am
  6. January 26, 2009 at 9:50 am
    • January 26, 2009 at 9:53 am
  7. Ben
    January 26, 2009 at 11:52 pm
    • February 5, 2009 at 8:56 am
  8. January 27, 2009 at 4:28 am
  9. February 5, 2009 at 8:54 am
  10. April 17, 2009 at 2:02 am
  11. April 17, 2009 at 9:43 pm
    • April 18, 2009 at 1:00 am
  12. Wladia Viviani
    April 18, 2009 at 5:34 pm
  13. florin
    April 19, 2009 at 9:43 am
    • April 19, 2009 at 6:12 pm
  14. Ian
    April 20, 2009 at 4:19 pm
  15. August 13, 2009 at 1:29 pm
  16. Rod
    August 15, 2009 at 12:34 pm
  17. RS
    September 17, 2009 at 11:21 pm
  18. September 19, 2009 at 3:58 pm
  19. October 29, 2009 at 12:05 am
  20. December 28, 2009 at 10:33 am
  21. Alex Bongo
    January 6, 2010 at 6:25 am
  22. February 26, 2010 at 8:26 pm
  23. March 1, 2010 at 11:26 pm

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates