Flipkart

Thursday, June 3, 2010

Output Buffering

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.

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;
}
?>

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.

No comments:

Post a Comment