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.
The behaviour of these functions is affected by settings in php.ini.
Table 1. Output Control configuration options
Name | Default | Changeable |
---|---|---|
output_buffering | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM |
output_handler | NULL | PHP_INI_PERDIR|PHP_INI_SYSTEM |
implicit_flush | "0" | PHP_INI_PERDIR|PHP_INI_SYSTEM |
Here is a short explanation of the configuration directives.
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).
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.
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.)
See also header(), setcookie() and .