Add to Favorites

PHP - Output Buffering

If you are writing some code and want to keep the html formatted nicely but need the html brought back and stored into a variable. Output buffering to the rescue!

Simply call ob_start to start an output buffer, do your html outside of php mode, then enter php mode again and get the value of that output buffer, like so:


<h1>Lots of html</h1>
text text text

$store_html = ob_get_clean();

It's that easy


Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up