views:

198

answers:

4

can anybody explain me what is output buffering and why one is using it in php?

+4  A: 

Output Buffering for Web Developers, a Beginner’s Guide:

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.
ax
+1. Here's another helpful link: http://php.net/manual/en/function.ob-start.php - also helpful when dealing with a function that echos a value that you'd rather store in a variable.
Cam
Is really everything buffered up to the end, or will the page just come in chunks if I have a reeeeally long page?
zedoo
@zedoo if you start output buffering with `ob_start()`, *really everything* is buffered. there is an optional second parameter to `ob_start()`, `int $chunk_size`, which, if set, will cause the buffer to be flushed after any output call which causes the buffer's length to equal or exceed this size.
ax
A: 

The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo() and data between blocks of PHP code.

http://php.net/manual/en/book.outcontrol.php

More Resources:

Output Buffering With PHP

Sarfraz
+3  A: 

Output buffering is used by PHP to improve performance and to perform a few tricks.

  • You can have PHP store all output into a buffer and output all of it at once improving network performance.

  • You can access the buffer content without sending it back to browser in certain situations.

Consider this example:

<?php
    ob_start( );
    phpinfo( );
    $vardump_result = ob_get_clean( );
?>

The above example captures the output into a variable instead of sending it to the browser. output_buffering is turned off by default.

  • You can use output buffering in situations when you want to modify headers after sending content.

Consider this example:

<?php
    ob_start( );
    echo "Hello World";
    if ( $some_error )
    {
        header( "Location: error.php" );
        exit( 0 );
    }
?>
Salman A
A: 

As name suggest here memory buffer used to manage how the output of script appears.

Here is one very good tutorial for the topic

nik