PHP: User-Level Output Buffers - Manual (original) (raw)
Table of Contents
- What Output Is Buffered?
- Nesting Output Buffers
- Buffer Size
- Operations Allowed On Buffers
- Output Handlers
- Working With Output Handlers
- Flags Passed To Output Handlers
- Output Handler Return Values
User-level output buffers can be started, manipulated and terminated from PHP code. Each of these buffers includes an output buffer and an associated output handler function.
Turning Output Buffering On
Output buffering can be turned on by using the ob_start() function or by setting the output_buffering and output_handler php.ini settings. While both can create output buffers,ob_start() is more flexible as it accepts user-defined functions as output handlers and the operations allowed on the buffer (flush, clean, remove) can be set as well. Buffers started with ob_start() will be active from the line the function was called, while those started withoutput_buffering will be buffering output from the first line of the script.
PHP is also shipped with a built-in "URL-Rewriter" output handler which starts its own output buffer and only allows up to two instances of it running at any time (one for user-level URL-rewriting and one for transparent session id support). These buffers can be started by calling the output_add_rewrite_var() function and/or by enabling thesession.use_trans_sid php.ini setting.
The bundled zlib extension has its own output buffer which can be enabled by using thezlib.output_compression php.ini setting.
Note: While
"URL-Rewriter"is special in that it only allows up to two instances of it running at any one time, all user-level output buffers use the same underlying buffers used by ob_start() with their functionality implemented by a custom output handler function. As such, all of their functionality can be emulated by userland code.
Flushing, Accessing And Cleaning Buffer Contents
Flushing sends and discards the contents of the active buffer. Output buffers get flushed when the size of the output exceeds the size of the buffer; the script ends orob_flush(), ob_end_flush() or ob_get_flush() is called.
Caution
Flushing buffers will flush the return value of the output handler which can differ from the contents of the buffer. For example, using ob_gzhandler() will compress the output and flush the compressed output.
The contents of the active buffer can be retrieved by callingob_get_contents(), ob_get_clean() or ob_get_flush().
If only the length of the buffer's contents are needed,ob_get_length() or ob_get_status() will return the length of the contents in bytes.
The contents of the active buffer can be cleaned by callingob_clean(), ob_end_clean() or ob_get_clean().
Turning Buffers Off
Output buffers can be turned off by callingob_end_clean(), ob_end_flush(),ob_get_flush() or ob_get_clean().
Every output buffer that has not been closed by the end of the script or when exit() is called will be flushed and turned off by PHP's shutdown process. The buffers will be flushed and turned off in reverse order of their starting up. The last buffered started will be first, the first buffer started will be last to be flushed and turned off.
Caution
If flushing of the buffer's contents is not desired, a custom output handler should be used to prevent flushing during shutdown.
Exceptions Thrown In Output Handlers
If an uncaught exception is thrown in an output handler the program terminates and the handler is invoked by the shutdown process after which the "Uncaught Exception" error message is flushed.
If the uncaught exception is thrown in a handler invoked by ob_flush(),ob_end_flush() or ob_get_flush(), the contents of the buffer are flushed before the error message.
If an uncaught exception is thrown in an output handler during shutdown, the handler is terminated and neither the contents of the buffer nor the error message is flushed.
Note: If a handler throws an exception its
[PHP_OUTPUT_HANDLER_DISABLED](outcontrol.constants.php#constant.php-output-handler-disabled)status flag is set.
Errors Raised In Output Handlers
If a non-fatal error is raised in an output handler the program continues execution.
If the non-fatal error is raised in a handler invoked byob_flush(), ob_end_flush() or ob_get_flush(), the buffer flushes certain data depending on the return value of the handler. If the handler returns [false](reserved.constants.php#constant.false) the buffer and the error message are flushed. If the returns anything else the handler return value is flushed but not the error message.
Note: If a handler returns
[false](reserved.constants.php#constant.false)its[PHP_OUTPUT_HANDLER_DISABLED](outcontrol.constants.php#constant.php-output-handler-disabled)status flag is set.
If a fatal error is raised in an output handler the program terminates and the handler is invoked by the shutdown process after which the error message is flushed.
If the fatal error is raised in a handler invoked by ob_flush(),ob_end_flush() or ob_get_flush(), the contents of the buffers are flushed before the error message.
If a fatal error is raised in an output handler during shutdown the program terminates without flushing the buffer or the error message.
Output In Output Handlers
In specific circumstances, output produced in the handler is flushed along with the contents of the buffer. This output is not appended to the buffer and is not part of the string returned by ob_get_flush().
During flush operations (calling ob_flush(),ob_end_flush(), ob_get_flush() and during shutdown) if the return value of a handler is [false](reserved.constants.php#constant.false) the contents of the buffer are flushed followed by the output. If the handler is not invoked during shutdown the handler throwing an exception or exit() being called results in the same behavior.
Note: If a handler returns
[false](reserved.constants.php#constant.false)its[PHP_OUTPUT_HANDLER_DISABLED](outcontrol.constants.php#constant.php-output-handler-disabled)status flag is set.
Found A Problem?
There are no user contributed notes for this page.