什么是输出缓冲,为什么要在PHP中使用它?
当前回答
顾名思义,这里的内存缓冲区用于管理脚本的输出如何出现。
这里有一个关于这个主题的很好的教程
其他回答
这是对php输出缓冲的总结。(XAMPP php.ini)
Output buffering is a mechanism for controlling how much output data (excluding headers and cookies) PHP should keep internally before pushing that data to the client. If your application's output exceeds this setting, PHP will send that data in chunks of roughly the size you specify. Turning on this setting and managing its maximum buffer size can yield some interesting side-effects depending on your application and web server. You may be able to send headers and cookies after you've already sent output through print or echo. You also may see performance benefits if your server is emitting less packets due to buffered output versus PHP streaming the output as it gets it. On production servers, 4096 bytes is a good setting for performance reasons.
注意:输出缓冲也可以通过输出缓冲控制来控制 功能。
可能的值:
On =启用,缓冲区不受限制。(请谨慎使用)
关=禁用
Integer =启用缓冲区,并以字节为单位设置缓冲区的最大大小。
注意:这个指令对于CLI SAPI是硬编码为Off的
默认值:关闭
开发价值:4096
产值:4096
http://php.net/output-buffering output_buffering = 4096
Web开发人员的输出缓冲,初学者指南:
Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. Advantages of output buffering for Web developers Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it's not being sent to the browser in pieces as PHP processes the HTML. All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable. If you've ever encountered the message "Warning: Cannot modify header information - headers already sent by (output)" while setting cookies, you'll be happy to know that output buffering is your answer.
PHP使用输出缓冲来提高性能并执行一些技巧。
您可以让PHP将所有输出存储到缓冲区中,并一次性输出所有输出,从而提高网络性能。 在某些情况下,您可以访问缓冲区内容而无需将其发送回浏览器。
想想这个例子:
<?php
ob_start( );
phpinfo( );
$output = ob_get_clean( );
?>
上面的示例将输出捕获到一个变量中,而不是将其发送到浏览器。默认情况下,Output_buffering是关闭的。
当您希望在发送内容后修改标头时,可以使用输出缓冲。
想想这个例子:
<?php
ob_start( );
echo "Hello World";
if ( $some_error )
{
header( "Location: error.php" );
exit( 0 );
}
?>
ob_start(); // turns on output buffering
$foo->bar(); // all output goes only to buffer
ob_clean(); // delete the contents of the buffer, but remains buffering active
$foo->render(); // output goes to buffer
ob_flush(); // send buffer output
$none = ob_get_contents(); // buffer content is now an empty string
ob_end_clean(); // turn off output buffering
缓冲区可以嵌套,因此当一个缓冲区处于活动状态时,另一个ob_start()会激活一个新的缓冲区。因此ob_end_flush()和ob_flush()并不是真正地将缓冲区发送到输出,而是发送到父缓冲区。只有当没有父缓冲区时,才会将内容发送到浏览器或终端。
这里有很好的解释:https://phpfashion.com/everything-about-output-buffering-in-php
输出控制函数允许您 来控制何时发送输出 脚本。这在 有几种不同的情况, 特别是当你需要发送头信息的时候 到浏览器后,您的脚本 开始输出数据。输出 控制功能不受影响 使用header()或 Setcookie(),仅用于 echo()和PHP块之间的数据 代码。
http://php.net/manual/en/book.outcontrol.php
更多资源:
使用PHP输出缓冲
推荐文章
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行