downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003

LXXVI. Output Control Functions


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.


No external libraries are needed to build this extension.


There is no installation needed to use these functions; they are part of the PHP core.

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 1. Output Control configuration options

For further details and definition of the PHP_INI_* constants see ini_set().

Here's a short explanation of the configuration directives.

output_buffering boolean/integer

You can enable output buffering for all files by setting this directive to 'On'. If you wish to limit the size of the buffer to a certain size - you can use a maximum number of bytes instead of 'On', as a value for this directive (e.g., output_buffering=4096).

output_handler string

You can redirect all of the output of your scripts to a function. For example, if you set output_handler to mb_output_handler(), character encoding will be transparently converted to the specified encoding. Setting any output handler automatically turns on output buffering.

Note: You cannot use both mb_output_handler() with ob_inconv_handler() and you cannot use both ob_gzhandler() and zlib.output_compression.

implicit_flush boolean

FALSE by default. Changing this to TRUE tells PHP to tell the output layer to flush itself automatically after every output block. This is equivalent to calling the PHP function flush() after each and every call to print() or echo() and each and every HTML block.

When using PHP within an web environment, turning this option on has serious performance implications and is generally recommended for debugging purposes only. This value defaults to TRUE when operating under the CLI SAPI.

See also ob_implicit_flush().

Resource Types

This extension has no resource types defined.

Predefined Constants

This extension has no constants defined.


Example 1. Output Control example


echo "Hello\n";

setcookie ("cookiename", "cookiedata");



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.)

Note: When upgrading from PHP 4.1 (and 4.2) to 4.3 that due to a bug in earlier versions you must ensure that implict_flush is OFF in your php.ini, otherwise any output with ob_start() will be not be hidden from output.

See Also

See also header() and setcookie().

Table of Contents
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_clean --  Get current buffer contents and delete current output buffer
ob_get_contents --  Return the 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

add a note add a note User Contributed Notes
Output Control Functions
09-Jul-2003 06:44
Trying to benchmark your server when using output_buffering ?
Don't forget that the value 4096 in the php.ini will give you complete different loadtimes compares to the value of 1.
In the first case the output will be sent after buffering 4096 and the loadtime timed at the end of the page will contain the loadtime needed to download the complete page in the clientbrowser while the second value will contain the loadtime needed to place the complete page in the buffer. The time needed for sending is not clocked.
This can be very frustrating if you don't see the differance between server and the 1st is using 4096 instead of 1.
Although technically much faster than the second server the second server was providing much better loadtime results.
This result will grow when using large amounts of output.
But this becomes interesting if you want to measure the time needed for the page to be loaded for the client.
jharris at rallycentral dot us
24-Jun-2003 01:40
To parse your output through HTMLtidy where PHP=>4.3.2, it is important to know about the fread() bug ( Previous versions had a bug which made the code "work as expected." In addition to fixing this bug, there are some others in the HTMLtidy parsing script posted here.

$gmt_modtime=gmdate('D, d M Y H:i:s', filemtime("/home/user/script.php") ). ' GMT';
    $fp=popen("echo \"" . $str . "\" | /usr/bin/tidy -config /home/user/htmlrc", "r");
    do {
        $data = fread($fp, 999999);
        if (strlen($data) == 0) {
        $newstr .= $data;
    header("Last-Modified: " . $gmt_modtime);
    header( "Content-length: " . strlen( stripslashes( $newstr ) ) );
    echo stripslashes($newstr);
cnc2dude at earthlink dot net
16-Dec-2002 06:57
Or just skip output buffering altogether and put:

$parsedtemplate = eval('return "' . addslashes(file_get_contents('templatefile.html')) . '";');
19-Nov-2002 01:40
Wouldn't this be easier?

$sf = fopen($sourcefile, "r");
$df = fopen($destfile, "w");

while( !feof($sf) )
$line = fgets($sf, 4096);
eval("$line = \"".addslashes($line)."\";");
fputs($df, stripslashes($line));
nathan at windsofstorm dot net
11-Jul-2002 03:13
It would seem to me that you could get what you wanted in that manner using object buffering and the readfile() function...
astericss60 at hotmail dot com
06-Apr-2002 11:25
You can use the ob_*-functions to replace the PHP variables in a file with its actual content. It is useful when you have a template file which includes some variables, like "User=$user" a.s.o. (Probably there is an easier way?):

$sf = fopen($sourcefile, "r");
$df = fopen($destfile, "w");

while( !feof($sf) )
    $line = fgets($sf, 4096);
    $eval = "echo \"".addslashes($line)."\";";
    $line = ob_get_contents();
    fputs($df, stripslashes($line));

Possibly, it helps someone...
fredrik at rambris dot com
18-Feb-2002 10:53
A cool way to use this is to run the output through HTML-tidy and get really nicelooking output without having to manually add indenting etc. Try this at the end (after doing an ob_start before outputting anything).

$fp=popen("echo \"" $str "\" | /usr/bin/tidy -i -u -q -latin1 --indent-spaces 1 -wrap 0""r");
Header"Content-length: " strlen$newstr ) );
philip at thepr()jects dot ()rg
09-Feb-2001 03:17

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST