
This article specially for web developers, I’ll trying to explain that how you get started with output buffering within seconds. This post is the beginning of a series in which I’ll share with you the BENEFITS of output buffering.
Simple points about Output Buffering
Without output buffering (the default), the HTML is sent to the browser in pieces, as PHP processes through the script. With output buffering, the HTML is stored in a variable and sent to the browser as one piece at the end of your script. Did you begin to see the performance advantages and post processing opportunities?
Advantages of output buffering
- 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.
// start output buffering at the top of our script with this simple command ob_start(); ?> Hello world! // end output buffering and send our HTML to the browser as a whole ob_end_flush(); ?>
It’s simple! Just by doing this our web pages appear less choppy as they render. Now let’s take it one step further.
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.
// start output buffering at the top of our script with this simple command
ob_start('ob_gzhandler'); // "ob_gzhandler" as a parameter of ob_start
?>
Hello world!
// 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!
// start output buffering at the top of our script with this simple command
ob_start('ob_postprocess'); //"ob_postprocess" (our custom post processing function) as a parameter of ob_start
?>
Hello world!
// 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.
Do take advantage of these tips of output buffering that you can plug into your own scripts.
I will publish the version 2 of this post soon in which I will try to cover some remaining tips. So stay tuned with this website.
Related posts:
- Image Magick and GD library installation on FEDORA server If you are dealing with the images in PHP then...
- 3 simple steps for automatic database backup This is a short tutorial that demonstrates that to take...
- Typography Solutions – Flir and sIFR I really not satisfied with the lack of support for...
- Ajax Validation with Jquery This is a very informative tutorial for developers who want...
- Memcached Server Installation and Access with PHP An exclusive tutorial about MEMCACHED Server, its simple installation method...
Related posts brought to you by Yet Another Related Posts Plugin.
























One Response
Wow this is a great resource.. I’m enjoying it.. good article