输出缓冲与将内容存储到PHP中的变量中 [英] Output buffering vs. storing content into variable in PHP

查看:65
本文介绍了输出缓冲与将内容存储到PHP中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确切知道输出缓冲的工作原理,但就其将内容存储到一些内部变量中而言.

I don't know exactly how output buffering works but as far as know it stores content into some internal variable.

关于这一点,不使用输出缓冲并将内容存储在我自己的局部变量中,而不是在脚本结尾处回显有什么区别?

Regarding this, what is the difference of not using output buffering and storing content in my own local variable instead and than echo it at the end of script?

输出缓冲示例:

<?php
  ob_start();
  echo "html page goes here";
  ob_end_flush();
?>

还有不使用输出缓冲的示例:

And example without using output buffering:

<?php
  $html = "html page goes here";
  echo $html;
?>

有什么区别?

推荐答案

主要区别:

1.)您可以使用常规"输出语法,例如echo语句.您不必重写问题.

1.) you can use "normal" output syntax, so for example an echo statement. You don't have to rewrite your problem.

2.)您可以更好地控制缓冲,因为可以堆叠缓冲区.您不必了解命名约定等,这使得在书写和使用方面彼此分开的情况下的实现更加容易.

2.) you have better control about the buffering, since buffers can be stacked. You don't have to know about naming conventions and the like, this makes implementations easier where the writing and using side are implemented separate from each other.

3.)只需输出flush,就不需要其他逻辑来输出缓冲的内容.如果输出流是特殊的东西,则特别有趣.为什么要负担控制范围呢?

3.) no additional logic require to output buffered content, you just flush. Especially interesting if the output stream is something special. Why burden the controlling scope with dealing with that?

4.),无论已创建输出缓冲区如何,都可以使用相同的输出实现.这是透明度的问题.

4.) you can use the same output implementation regardless of an output buffer has been created. THis is a question of transparency.

5.)您可以意外捕获气泡"之类的东西,例如警告之类的东西,然后再将其吞下即可.

5.) you can 'catch' accidentially out bubbled stuff like warnings and the like and simply swallow it afterwards.

[...]

这篇关于输出缓冲与将内容存储到PHP中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆