PHP - Output Buffering

Post

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

Leave a comment

November 2026
Sun
Mon
Tue
Wed
Thu
Fri
Sat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Share