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 callable may be specified. It can also be bypassed by passing null.
callback is invoked when the output buffer is flushed (sent), cleaned, or when the output buffer is flushed at the end of the script.
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 the contents of the buffer are returned.
WARNING: Calling any of the following functions from within an output handler will result in a fatal error: ob_clean(), ob_end_clean(), ob_end_flush(), ob_flush(), ob_get_clean(), ob_get_flush(), ob_start().
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