Contents / Previous / Next


Controling the Output Buffer

The Output Control functions allow you to control when output is sent from the script to the browser. Example: <? // Try with in a shell with the php command: $text = "slowly it is coming out...."; for( $i=0; $i<strlen($text); $i++ ) { ob_start(); print substr( $text, $i, 1 ); sleep(1); // sleeptime in seconds. ob_end_flush(); } ?>
Output buffering can be useful in several different situations, for example, to backup the output in a string or for handling cookies.

Example:

<? ob_start(); echo "Hello\n"; setcookie ("cookiename", "cookiedata"); ob_end_flush(); ?> In the above example, the output from echo() would be stored in the output buffer until ob_end_flush() was called. In the mean time, the call to setcookie() successfully stored a cookie without causing an error. (You can not normally send headers to the browser after data has already been sent.)


Output Buffer Control Functions

flush         :  Flush the output buffer
ob_clean      :  Clean (erase) the output buffer
ob_end_clean  :  Clean (erase) the output buffer 
                 and turn off output buffering
ob_end_flush  :  Flush (send) the output buffer 
                 and turn off output buffering
ob_flush      :  Flush (send) the output buffer
ob_get_contents :  Return contents of the output buffer
ob_get_length :  Return the length of the output buffer
ob_get_level  :  Return the nesting level of the 
                 output buffering mechanism
ob_get_status :  Get status of output buffers
ob_gzhandler  :  ob_start callback function 
                 to gzip output buffer
ob_implicit_flush :  Turn implicit flush on/off
ob_start      : Turn on output buffering