8

In PHP sometimes I see this:

$html = <<<HTML
<p>Hello world</p>
HTML;

Normally I would have used ob_start() :

ob_start();
?>
<p>Hello world</p>
<?php
$html = ob_get_contents();
ob_clean();

Can you tell me what the difference between these two ways to write to the buffer and their advantages?

Jedi
  • 397
  • 2
  • 4
  • 11
  • 5
    I'd say that HEREDOCs are obviously a lot cleaner. It's also not "different ways to write to the buffer". HEREDOCs are a way of *quoting a string*. `ob_` is a, dare I say, hack to capture any output before it goes out. – deceze Nov 19 '12 at 14:13
  • 1
    Agree with deceze, most use of `ob_*` functions point to bad design. Then again HEREDOC's scream 'view layer' in either the controller or model. – Mike B Nov 19 '12 at 14:14
  • @MikeB it also screams "Heeey give me money for another coder!" – jave.web Jan 05 '14 at 18:30
  • 1
    @deceze @MikeB [this thread](http://programmers.stackexchange.com/a/159531/146747) states that the use of `ob_*` is common in templating systems, where HEREDOCS would not work. – Ulad Kasach May 17 '16 at 15:29

2 Answers2

8
$html = <<<HTML
<p>Hello world</p>
HTML;
// equivalent:
$html = "<p>Hello world</p>";

This uses the PHP string Heredoc syntax, which is a syntax to write a string, similar to using single quotes and double quotes but escape things in a somehow different way. You can use {} to directly insert some PHP strings into it.


<?php
ob_start();
?>
<p>Hello world</p>
<?php
$html = ob_get_clean();

This is a totally different thing. It makes use of the PHP output buffering control to capture things that are not inside PHP code blocks. Like in the given example, <p>Hello world</p> is written outside the PHP code block, which is supposed to be output to the client immediately. With output buffering enabled they are stored inside a buffer in PHP, so that it can be retrieved later using ob_get_contents() or ob_get_clean(). If you need to insert any PHP variables, you need to use <?=$blah?> or even <?php echo $blah?>.


Some CMS use the output buffering control functions to manage contents and modules. One example is Joomla. The advantage of this design is that whenever the module need to place content to its reserved space, it can simply use echo to make the content available. That can simplify the way to obtain contents from the modules, without needing to implement a specific function call or assign to a specific variable, which makes the system easier to manage.

<?php
ob_start();
include dirname(__FILE__)."/content.php";
$content = ob_get_clean();
output_document(array("body"=>$content));

I also make use of output buffering functions such that I can include one file on the top, and without having any PHP at the end I can create a simple template system, but this is sort of off-topic.

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
  • Joomla is using it? Thanks for the info - now I really know that using outputbuffering for storing html in variable is a bad design :) (Joomla seems too messy/bad designed for me :) ) – jave.web Jan 05 '14 at 18:33
6

HEREDOC (<<<) is just another way to write a string data into a variable. The output buffer, on the other hand, will catch all output that takes place after ob_start() including (HTML) output of any warnings or errors you might have in the code before you call ob_get_contents();

Usually, if you just need to format a string with HTML, just use HEREDOC or regular string notation. Output buffer is usually used if you need to catch output before you send any HTTP headers (for an example, if you're using FirePHP to debug your application, you'll need to use output buffering because FirePHP embeds the logging data in the HTTP headers).

Martin Vrkljan
  • 879
  • 10
  • 20
  • 1
    Thank you guys. I see the point. HEREDOCs is good for simple HTML templates whereas ob_start(), ob_get_contents() grabs everything in between. – Jedi Nov 19 '12 at 14:30