ob_start
Description
The ob_start of Output Control for PHP turn on output buffering.
Syntax
ob_start( callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS ): bool
Parameters
callback
An optional callback function may be specified. This function takes a string as a parameter and should return a string. The function will be called when the output buffer is flushed (sent) or cleaned (with ob_flush(), ob_clean() or similar function) or when the output buffer is flushed to the browser at the end of the request. When callback is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser. If the callback is not a callable function, this function will return false. This is the callback signature:
handler( string $buffer, int $phase = ? ): string
buffer
Contents of the output buffer.
phase
Bitmask of PHP_OUTPUT_HANDLER_* constants.
Constant | Value | Description |
---|---|---|
PHP_OUTPUT_HANDLER_START | 1 | ouput buffer has started |
PHP_OUTPUT_HANDLER_FLUSH | 4 | ouput buffer is being flushed |
PHP_OUTPUT_HANDLER_FINAL | 8 | ouput buffer will be ended |
If callback returns false original input is sent to the browser.
The callback parameter may be bypassed by passing a null value.
ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() may not be called from a callback function. If you call them from callback function, the behavior is undefined. If you would like to delete the contents of a buffer, return "" (a null string) from callback function. You can't even call functions using the output buffering functions like print_r($expression, true) or highlight_file($filename, true) from a callback function.
chunk_size
If the optional parameter chunk_size is passed, the buffer will be flushed after any output call which causes the buffer's length to equal or exceed chunk_size. The default value 0 means that the output function will only be called when the output buffer is closed.
flags
The flags parameter is a bitmask that controls the operations that can be performed on the output buffer. The default is to allow output buffers to be cleaned, flushed and removed, which can be set explicitly via PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE, or PHP_OUTPUT_HANDLER_STDFLAGS as shorthand.
Each flag controls access to a set of functions, as described below:
Constant | Number | Functions |
---|---|---|
PHP_OUTPUT_HANDLER_CLEANABLE | 16 | ob_clean(), ob_end_clean(), ob_get_clean() |
PHP_OUTPUT_HANDLER_FLUSHABLE | 32 | ob_end_flush(), ob_flush(), ob_get_flush() |
PHP_OUTPUT_HANDLER_REMOVABLE | 64 | ob_end_clean(), ob_end_flush(), ob_get_flush() |
PHP_OUTPUT_HANDLER_STDFLAGS | 112 | Default. PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE | PHP_OUTPUT_HANDLER_REMOVABLE |
Return
Returns true on success or false on failure.
Examples
1 · void
<? ob_start(); echo 'output'; ?>
output
2 · callback · buffer
<? function handler($buffer) { return "buffer: " . $buffer; } $callback = 'handler'; ob_start($callback); echo 'output'; ?>
buffer: output
3 · callback · phase
<? function handler($buffer, $phase) { return "buffer: " . $buffer . PHP_EOL . "phase: " . $phase; } $callback = 'handler'; ob_start($callback); echo 'output'; ?>
buffer: output phase: 9
4 · chunk_size
<? $callback = null; $chunk_size = 1024; ob_start($callback, $chunk_size); echo 'output'; ?>
output
5 · flags · PHP_OUTPUT_HANDLER_CLEANABLE
<? $callback = null; $chunk_size = 0; $flags = PHP_OUTPUT_HANDLER_CLEANABLE; ob_start($callback, $chunk_size, $flags); echo 'ob_clean' . PHP_EOL; ob_clean(); ob_start($callback, $chunk_size, $flags | PHP_OUTPUT_HANDLER_REMOVABLE); echo 'ob_end_clean' . PHP_EOL; ob_end_clean(); ob_start($callback, $chunk_size, $flags | PHP_OUTPUT_HANDLER_REMOVABLE); echo 'ob_get_clean' . PHP_EOL; $output = ob_get_clean(); ?>
6 · flags · PHP_OUTPUT_HANDLER_FLUSHABLE
<? $callback = null; $chunk_size = 0; $flags = PHP_OUTPUT_HANDLER_FLUSHABLE; ob_start($callback, $chunk_size, $flags); echo 'ob_end_flush' . PHP_EOL; ob_end_flush(); ob_start($callback, $chunk_size, $flags); echo 'ob_flush' . PHP_EOL; ob_flush(); ob_start($callback, $chunk_size, $flags); echo 'ob_get_flush' . PHP_EOL; $output = ob_get_flush(); ?>
ob_end_flush ob_flush ob_get_flush
7 · flags · PHP_OUTPUT_HANDLER_REMOVABLE
<? $callback = null; $chunk_size = 0; $flags = PHP_OUTPUT_HANDLER_REMOVABLE; ob_start($callback, $chunk_size, $flags); echo 'ob_end_clean' . PHP_EOL; ob_end_clean(); ob_start($callback, $chunk_size, $flags); echo 'ob_end_flush' . PHP_EOL; ob_end_flush(); ob_start($callback, $chunk_size, $flags); echo 'ob_get_flush' . PHP_EOL; $output = ob_get_flush(); ?>
ob_end_flush ob_get_flush
8 · flags · PHP_OUTPUT_HANDLER_STDFLAGS
<? $callback = null; $chunk_size = 0; $flags = PHP_OUTPUT_HANDLER_STDFLAGS; ob_start($callback, $chunk_size, $flags); echo 'ob_clean' . PHP_EOL; ob_clean(); ob_start($callback, $chunk_size, $flags); echo 'ob_end_clean' . PHP_EOL; ob_end_clean(); ob_start($callback, $chunk_size, $flags); echo 'ob_end_flush' . PHP_EOL; ob_end_flush(); ob_start($callback, $chunk_size, $flags); echo 'ob_flush' . PHP_EOL; ob_flush(); ob_start($callback, $chunk_size, $flags); echo 'ob_get_clean' . PHP_EOL; $output = ob_get_clean(); ob_start($callback, $chunk_size, $flags); echo 'ob_get_flush' . PHP_EOL; $output = ob_get_flush(); ?>
ob_end_flush ob_flush ob_get_flush
9 · return
<? $return = ob_start(); var_export($return); ?>
true