Jedi Jedi - 2 years ago 160
PHP Question

PHP: <<< vs ob_start

In PHP sometimes I see this:

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

Normally I would have used ob_start() :

<p>Hello world</p>
$html = ob_get_contents();

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

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download