ob_start()
ob_start() — Turn on output buffering.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
This function will turn output buffering on. While output buffering is active no output is sent from the script, instead the output is stored in an internal buffer. See What Output Is Buffered? on exactly what output is affected.
Signature
ob_start(?callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS): bool handler(string $buffer, int $phase = ?): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $callback | ?callable | An optional callback callable may be specified. It can also be bypassed by passing null. |
| $chunk_size | int | If the optional parameter chunk_size is passed, the buffer will be flushed after any block of code resulting in output that causes the buffer's length to equal or exceed chunk_size. The default value 0 means that all output is buffered until the buffer is turned off. A value of 1 means the buffer will be flushed after every output operation, effectively disabling buffering. chunk_size also determines the initial ... |
| $flags | int | 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 the buffer control flags . See Operations Allowed On Buffers for more details. |
Return value
Returns true on success or false on failure.
Example
A runnable snippet:
function callback($buffer) { // replace all the apples with oranges return (str_replace("apples", "oranges", $buffer)); } ob_start("callback"); ?> <html> <body> <p>It's like comparing apples to oranges.</p> </body> </html> <?php ob_end_flush();
Output:
<html> <body> <p>It's like comparing oranges to oranges.</p> </body> </html>
Changelog
| Version | Change |
|---|---|
| 8.5.0 | Trying to produce output (e.g. with echo) within the user output handler callback is now deprecated. The deprecation warning bypasses the handler and is output directly. |
See also
ob_get_contents(), ob_end_clean(), ob_end_flush(), ob_implicit_flush(), ob_gzhandler(), mb_output_handler()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page ob_start() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.