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:

ob_start();
?>

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

<?php
$store_html = ob_get_clean();

It's that easy

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up