简体   繁体   English

PHP:<<< vs ob_start

[英]PHP: <<< vs ob_start

In PHP sometimes I see this: 在PHP中有时候我会看到:

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

Normally I would have used ob_start() : 通常我会使用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? 你能告诉我这两种写入缓冲区的方式和它们的优点有什么区别吗?

$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. 这使用PHP字符串Heredoc语法 ,这是一种编写字符串的语法,类似于使用单引号和双引号但以某种方式以某种方式转义事物。 You can use {} to directly insert some PHP strings into it. 您可以使用{}直接在其中插入一些PHP字符串。


<?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. 它利用PHP输出缓冲控件来捕获不在 PHP代码块内的东西。 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. 与给定的示例一样, <p>Hello world</p>是在PHP代码块之外编写的,它应该立即输出到客户端。 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() . 启用输出缓冲后,它们将存储在PHP中的缓冲区中,以便稍后可以使用ob_get_contents()ob_get_clean()来检索它。 If you need to insert any PHP variables, you need to use <?=$blah?> or even <?php echo $blah?> . 如果需要插入任何PHP变量,则需要使用<?=$blah?>或甚至<?php echo $blah?>


Some CMS use the output buffering control functions to manage contents and modules. 某些CMS使用输出缓冲控制功能来管理内容和模块。 One example is Joomla. 一个例子是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. 这种设计的优点是,无论何时模块需要将内容放置到其保留空间,它都可以简单地使用echo来使内容可用。 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. 我还使用输出缓冲功能,这样我可以在顶部包含一个文件,最后我没有任何PHP,我可以创建一个简单的模板系统,但这有点偏离主题。

HEREDOC ( <<< ) is just another way to write a string data into a variable. HEREDOC( <<< )只是将字符串数据写入变量的另一种方法。 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() ; 另一方面,输出缓冲区将捕获ob_start()之后发生的所有输出,包括(HTML)输出您在调用ob_get_contents()之前可能在代码中出现的任何警告或错误;

Usually, if you just need to format a string with HTML, just use HEREDOC or regular string notation. 通常,如果您只需要使用HTML格式化字符串,只需使用HEREDOC或常规字符串表示法。 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). 如果您需要在发送任何HTTP标头之前捕获输出,则通常使用输出缓冲区(例如,如果您使用FirePHP调试应用程序,则需要使用输出缓冲,因为FirePHP将日志记录数据嵌入到HTTP中头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM