Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: <<< vs ob_start

Tags:

php

buffering

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?

like image 722
Jedi Avatar asked Nov 19 '12 14:11

Jedi


People also ask

When should I use Ob_start?

Using ob_start allows you to keep the content in a server-side buffer until you are ready to display it. This is commonly used to so that pages can send headers 'after' they've 'sent' some content already (ie, deciding to redirect half way through rendering a page).

What does Ob_start () do in PHP?

The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.

What is Ob_start () and Ob_end_flush () in PHP?

While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush().

What is use of Ob_end_clean in PHP?

The ob_end_clean() function deletes the topmost output buffer and all of its contents without sending anything to the browser.


1 Answers

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

like image 173
Alvin Wong Avatar answered Sep 22 '22 13:09

Alvin Wong