Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ob flush example
<?php
/*
== ob_end_flush() ==
Flush (send) the output buffer and turn off output buffering
== ob_end_clean() ==
Clean (erase) the output buffer and turn off output buffering
== ob_get_contents() ==
Return the contents of the output buffer
*/
# The following two functions result in the same output.
function output_some_html_with_var() {
ob_start();
?>
<p>This is a paragraph echoed from a variable.</p>
<?php
$para = ob_get_contents();
ob_end_clean();
echo $para;
}
function output_some_html_with_end_flush() {
ob_start();
?>
<p>This is a paragraph sent with ob_end_flush.</p>
<?php
ob_end_flush();
}
output_some_html_with_var();
output_some_html_with_end_flush();
/*
If you want to return the contents of ob_start()
you should store ob_get_contents() into a variable,
then call ob_end_clean(), and then return the variable.
*/
function return_some_html_with_var() {
ob_start();
?>
<p>This is a paragraph being stored into a variable and returned.</p>
<?php
$para = ob_get_contents();
ob_end_clean();
return $para;
}
echo return_some_html_with_var();
echo '<a style="text-decoration:none;" href="http://php.net/manual/en/function.ob-start.php">See the ob_start documentiation for more info</a>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.