ob_start()是否用于输出缓冲,以便头被缓冲而不发送到浏览器?我说的有道理吗?如果不是,那么为什么我们要使用ob_start()?
当前回答
这里公认的答案描述了ob_start()做什么-而不是为什么使用它(这是被问到的问题)。
如别处所述,ob_start()创建一个缓冲区,输出写入其中。
但是没有人提到在PHP中可以堆叠多个缓冲区。看到ob_get_level()。
至于为什么....
Sending HTML to the browser in larger chunks gives a performance benefit from a reduced network overhead. Passing the data out of PHP in larger chunks gives a performance and capacity benefit by reducing the number of context switches required Passing larger chunks of data to mod_gzip/mod_deflate gives a performance benefit in that the compression can be more efficient. buffering the output means that you can still manipulate the HTTP headers later in the code explicitly flushing the buffer after outputting the [head]....[/head] can allow the browser to begin marshaling other resources for the page before HTML stream completes. Capturing the output in a buffer means that it can redirected to other functions such as email, or copied to a file as a cached representation of the content
其他回答
可以把ob_start()看作是在说“开始记住通常会输出的所有内容,但还没有对它做任何事情。”
例如:
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_contents();
ob_end_clean();
还有另外两个函数通常与它配对:ob_get_contents(),它基本上提供了自ob_start()打开缓冲区以来“保存”到缓冲区的任何内容,然后是ob_end_clean()或ob_flush(),它们分别停止保存并丢弃保存的内容,或停止保存并一次性输出。
我使用这个,所以我可以用很多HTML跳出PHP,但不渲染它。它使我不必将它存储为禁用IDE颜色编码的字符串。
<?php
ob_start();
?>
<div>
<span>text</span>
<a href="#">link</a>
</div>
<?php
$content = ob_get_clean();
?>
而不是:
<?php
$content = '<div>
<span>text</span>
<a href="#">link</a>
</div>';
?>
这是为了进一步澄清JD Isaaks的答案…
您经常遇到的问题是,您使用php从许多不同的php源输出html,而出于某种原因,这些源通常通过不同的方式输出。
有时你有文字html内容,你想直接输出到浏览器;其他时候输出是动态创建的(服务器端)。
动态内容总是一个字符串。现在你必须结合这个字符串化的动态html与任何文字,直接显示html…转换成一个有意义的HTML节点结构。
这通常迫使开发人员将所有直接显示的内容包装成一个字符串(就像JD Isaak讨论的那样),这样它就可以与动态html一起正确地交付/插入……即使你不想把它包起来。
但是通过使用ob_##方法,可以避免字符串换行的混乱。相反,文字内容输出到缓冲区。然后在一个简单的步骤,整个内容的缓冲区(所有文字html),连接到您的动态html字符串。
(我的例子显示文字html输出到缓冲区,然后添加到一个html-string…也可以看看JD Isaaks的例子,看看字符串- wrapped -of-html)。
<?php // parent.php
//---------------------------------
$lvs_html = "" ;
$lvs_html .= "<div>html</div>" ;
$lvs_html .= gf_component_assembler__without_ob( ) ;
$lvs_html .= "<div>more html</div>" ;
$lvs_html .= "----<br/>" ;
$lvs_html .= "<div>html</div>" ;
$lvs_html .= gf_component_assembler__with_ob( ) ;
$lvs_html .= "<div>more html</div>" ;
echo $lvs_html ;
// 02 - component contents
// html
// 01 - component header
// 03 - component footer
// more html
// ----
// html
// 01 - component header
// 02 - component contents
// 03 - component footer
// more html
//---------------------------------
function gf_component_assembler__without_ob( )
{
$lvs_html = "<div>01 - component header</div>" ; // <table ><tr>" ;
include( "component_contents.php" ) ;
$lvs_html .= "<div>03 - component footer</div>" ; // </tr></table>" ;
return $lvs_html ;
} ;
//---------------------------------
function gf_component_assembler__with_ob( )
{
$lvs_html = "<div>01 - component header</div>" ; // <table ><tr>" ;
ob_start();
include( "component_contents.php" ) ;
$lvs_html .= ob_get_clean();
$lvs_html .= "<div>03 - component footer</div>" ; // </tr></table>" ;
return $lvs_html ;
} ;
//---------------------------------
?>
<!-- component_contents.php -->
<div>
02 - component contents
</div>
不,你错了,但方向是合适的;)
output - buffering缓冲脚本的输出。这就是(简而言之)回声或打印后的一切。头部的事情是,他们只能被发送,如果他们还没有被发送。但是HTTP表示,头信息是传输的第一步。因此,如果你第一次(在请求中)输出某些内容,则发送头信息,并且不能设置其他头信息。
我喜欢:
ob_start();
echo("Hello there!");
$output = ob_get_clean(); //Get current buffer contents and delete current output buffer
推荐文章
- 如何实现一个好的脏话过滤器?
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PHP中获得变量名作为字符串?
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 在PHP中对动态变量名使用大括号
- 如何从对象数组中通过对象属性找到条目?
- 如何从关联数组中删除键及其值?
- PHP字符串中的花括号