ob_start() / ob_get_clean()
| Since: | PHP 4(2000) |
|---|
Functions for controlling output buffering. In PHP, sending any output before calling header() causes an error. Output buffering lets you hold all output in a temporary buffer to avoid this problem. Output from echo, print_r(), and similar functions is stored in the buffer and can be processed all at once.
Syntax
// Starts output buffering. ob_start($callback, $chunk_size, $flags); // Returns the contents of the buffer. The buffer is kept intact. ob_get_contents(); // Returns the contents of the buffer and ends buffering. ob_get_clean(); // Outputs the contents of the buffer and ends buffering. ob_end_flush();
Function List
| Function | Description |
|---|---|
| ob_start($callback) | Starts output buffering. All subsequent output is stored in the buffer and not displayed on screen. You can also pass a callback function to transform the buffer contents. |
| ob_get_contents() | Returns the current buffer contents as a string. The buffer is not cleared and remains intact. |
| ob_get_clean() | Returns the buffer contents as a string and ends buffering. Equivalent to calling ob_get_contents() followed by ob_end_clean(). |
| ob_end_flush() | Outputs the buffer contents and ends buffering. Use this when you want to send all buffered output to the screen at once. |
| ob_end_clean() | Discards the buffer contents and ends buffering. Nothing is output. |
| ob_get_level() | Returns the current nesting level of output buffering. Returns 0 if buffering is not active. |
Sample Code
sample_ob_start.php
<?php
// Basic usage of output buffering.
ob_start();
echo "This output is stored in the buffer.";
echo "It has not been displayed on screen yet.";
$content = ob_get_clean(); // Retrieves the buffer contents and ends buffering.
echo mb_strlen($content, "UTF-8") . " characters of output were captured.";
// Capturing var_dump() output as a string.
ob_start();
var_dump(['PHP', 'JavaScript', 'Python']);
$debug = ob_get_clean();
error_log($debug); // Writes the debug info to the log.
// Using output buffering to generate HTML templates.
function render_card($title, $body) {
ob_start();
echo '';
echo '' . htmlspecialchars($title) . '
';
echo '' . htmlspecialchars($body) . '
';
echo '';
return ob_get_clean();
}
$html = render_card("Notice", "The site has been relaunched.");
echo $html;
// Suppressing output before sending a header() redirect.
ob_start();
echo "This output will be discarded before the redirect.";
ob_end_clean(); // Discards the buffer.
header("Location: /login.php");
exit;
// Using a callback to transform output.
ob_start(function ($buffer) {
return mb_strtoupper($buffer); // Converts all output to uppercase.
});
echo "hello world";
ob_end_flush(); // Outputs 'HELLO WORLD'.
php sample_ob_start.php 76 characters of output were captured. <div class="card"><h3><span class="marker-yellow">Notice</span></h3><p>The site has been relaunched.</p></div>
The var_dump() output is written to the error log via error_log() and does not appear in standard output. After that, ob_end_clean() discards the buffer and header() performs a redirect. Running the callback example (ob_end_flush()) would output 'HELLO WORLD'.
Overview
ob_start() begins output buffering, storing all subsequent output from echo, print_r(), and similar functions into an in-memory buffer. The buffered output can then be retrieved as a string with ob_get_clean(), or sent to the browser all at once with ob_end_flush().
Common uses for output buffering include capturing var_dump() output as a string, rendering partial HTML templates, and suppressing output before a header() call. In PHP, any output sent before header() or setcookie() causes an error — output buffering lets you avoid this problem.
Buffering can be nested, and you can check the current nesting depth with ob_get_level(). For debug output, see var_dump(). For basic output, see echo / print.
If you find any errors or copyright issues, please contact us.