Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created October 11, 2017 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/05738e4b99777d378159546b6feffaa8 to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/05738e4b99777d378159546b6feffaa8 to your computer and use it in GitHub Desktop.
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