Basic usage getting content between buffers and clearing
suggest changeOutput buffering lets you store textual content, including HTML, in a variable and send it to the browser as one piece at the end of your script. By default, PHP sends content as it is produced.
<?php
// Turn on output buffering
ob_start();
// Print some output to the buffer (via php)
print 'Hello ';
// You can also `step out` of PHP
?>
<em>World</em>
<?php
// Return the buffer AND clear it
$content = ob_get_clean();
// Return our buffer and then clear it
# $content = ob_get_contents();
# $did_clear_buffer = ob_end_clean();
print($content); // Hello \n<em>World</em>\n
Whitespace outside <?php ?>, including the newlines around <em>World</em>, is part of the buffer. Anything output between ob_start() and ob_get_clean() is stored in $content.
Calling ob_get_clean() triggers both ob_get_contents() and ob_end_clean().
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents