PHP Reference ยท Files and I/O
PHP output buffering
Reference for PHP output buffering: ob_start(), ob_get_clean(), ob_end_flush(), nested buffers, callbacks, and why headers can be sent after echo.
Normally echo and print send text straight to the output. With output buffering turned on, PHP collects that text in memory instead. You can then inspect it, change it, throw it away or send it later. It is the standard way to capture the output of code that prints (such as a template) into a string.
Buffers stack: each ob_start() adds a new layer, and the functions below always act on the topmost one.
<?php ob_start(); echo "hidden for now"; $text = ob_get_clean(); echo strtoupper($text);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
ob_start(?callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS): bool ob_get_contents(): string|false ob_get_clean(): string|false ob_get_flush(): string|false ob_end_clean(): bool ob_end_flush(): bool ob_clean(): void ob_flush(): void ob_get_level(): int ob_get_length(): int|false
| Function | What it does |
|---|---|
ob_start() |
Starts a new buffer. An optional callback receives the buffer contents when it is flushed and returns the text to send on. |
ob_get_contents() |
Returns the buffer without touching it. |
ob_get_clean() |
Returns the buffer and discards it (get + end). |
ob_get_flush() |
Returns the buffer, flushes it to the next layer and ends it. |
ob_end_clean() / ob_end_flush() |
Ends the topmost buffer, discarding or flushing it. |
ob_clean() / ob_flush() |
Discards or flushes the contents but keeps the buffer active. |
ob_get_level() |
Number of active buffers (0 when none). |
Return value: the ob_get_* functions return the contents as a string, or false if no buffer is active. ob_start() returns true.
Errors / Exceptions: ob_end_clean() and ob_end_flush() return false and emit a notice when there is no buffer to end.
Usage notes
- Match every start with an end. Unclosed buffers are flushed when the script ends, but leaving them open on purpose makes code hard to follow. Prefer
ob_get_clean()in helper functions. - Headers. Output is normally sent before headers can be changed; while a buffer holds the text,
header()andsetcookie()still work. This is why some old code usesob_start()at the top of a file to avoid "headers already sent". - Callbacks run on flush, not on each
echo. A callback can minify HTML or add a footer, but must not call output functions itself. chunk_size: with a value above 0 the buffer flushes whenever it grows past that many bytes. That reduces memory use but also makes the callback run several times.- Many servers enable buffering through the
output_bufferingini setting. Command-line PHP ignores it. php://outputgoes through the buffers, whilephp://stdoutbypasses them; see php:// streams.- Exceptions. If an exception escapes while a helper has an open buffer, the partial output stays buffered. Use
try/finallywithob_end_clean()when failure is possible. - Streaming and progress output need the opposite:
flush()together withob_flush(), or buffering disabled.
Related
- php:// streams,
echo,print,ini_set() - Lessons: PHP and HTML, HTTP Headers and Redirects