什么是输出缓冲,为什么在 PHP 中使用它?
8 回答
如果没有输出缓冲(默认设置),您的 HTML 将在 PHP 通过您的脚本处理时分段发送到浏览器。使用输出缓冲,您的 HTML 存储在一个变量中,并在脚本末尾作为一个片段发送到浏览器。
输出缓冲对 Web 开发人员的优势
- 单独打开输出缓冲可以减少下载和渲染 HTML 所需的时间,因为它不会在 PHP 处理 HTML 时被分段发送到浏览器。
- 我们可以用 PHP 字符串做的所有花哨的事情,现在我们可以将整个 HTML 页面作为一个变量来做。
- 如果您在设置 cookie 时遇到消息“警告:无法修改标头信息 - 标头已由(输出)发送”,您会很高兴知道输出缓冲是您的答案。
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 );
}
?>
我知道这是一个老问题,但我想为视觉学习者写下我的答案。我在全球网络上找不到任何解释输出缓冲的图表,所以我自己在 Windows 中制作了一个图表mspaint.exe
。
如果输出缓冲关闭,那么echo
将立即向浏览器发送数据。
如果打开了输出缓冲,那么 anecho
将在将数据发送到浏览器之前将数据发送到输出缓冲区。
phpinfo
要查看输出缓冲是否打开/关闭,请参阅核心部分的 phpinfo。该output_buffering
指令将告诉您输出缓冲是否打开/关闭。
在这种情况下,该
output_buffering
值为 4096,这意味着缓冲区大小为 4 KB。这也意味着在 Web 服务器上打开了输出缓冲。
php.ini
output_buffering
可以通过更改指令的值来打开/关闭和更改缓冲区大小。只需在 中找到它php.ini
,将其更改为您选择的设置,然后重新启动 Web 服务器。您可以在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.
; Note: Output buffering can also be controlled via Output Buffering Control
; functions.
; Possible Values:
; On = Enabled and buffer is unlimited. (Use with caution)
; Off = Disabled
; Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
; http://php.net/output-buffering
output_buffering = 4096
该指令output_buffering
不是关于输出缓冲的唯一可配置指令。您可以在此处找到其他可配置的输出缓冲指令:http: //php.net/manual/en/outcontrol.configuration.php
示例:ob_get_clean()
您可以在下面看到如何echo
在将其发送到浏览器之前对其进行捕获和操作。
// Turn on output buffering
ob_start();
echo 'Hello World'; // save to output buffer
$output = ob_get_clean(); // Get content from the output buffer, and discard the output buffer ...
$output = strtoupper($output); // manipulate the output
echo $output; // send to output stream / Browser
// OUTPUT:
HELLO WORLD
示例:Hackingwithphp.com
可以在此处找到有关带有示例的输出缓冲区的更多信息:
输出控制功能允许您控制何时从脚本发送输出。这在几种不同的情况下很有用,尤其是当您需要在脚本开始输出数据后将标头发送到浏览器时。输出控制函数不影响使用 header() 或 setcookie() 发送的标头,仅影响诸如 echo() 之类的函数和 PHP 代码块之间的数据。
http://php.net/manual/en/book.outcontrol.php
更多资源:
顾名思义,这里的内存缓冲区用于管理脚本输出的显示方式。
这是该主题的一个非常好的教程
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
2019 年更新。如果您有专用服务器和 SSD 或更好的 NVM,则为 3.5GHZ。您不应该使用缓冲在 100 毫秒到 150 毫秒内使网站加载速度更快。
因为网络比 2019 年使用性能服务器(服务器、内存、磁盘)和打开 APC PHP 来处理脚本慢 :) 生成的脚本有时只需要 70 毫秒,而另一种时间只是网络需要时间,从定位到 10 毫秒到 150 毫秒用户服务器。
所以如果你想快 150 毫秒,缓冲会变慢,因为需要额外的收集缓冲数据,它会产生额外的成本。10 年前服务器制作 1s 脚本时,它很有用。
如果您想使用 jpg 加载它可以刷新自动和崩溃发送,请注意 output_buffering 有限制。
干杯。
您可以制作快速河流,或者您可以安全制作 tama :)
这是 php 中输出缓冲的总结。(XAMPP php.ini)
输出缓冲是一种机制,用于控制 PHP 在将数据推送到客户端之前应在内部保留多少输出数据(不包括标头和 cookie)。如果您的应用程序的输出超出此设置,PHP 将发送该数据大致为您指定大小的块。启用此设置并管理其最大缓冲区大小可能会产生一些有趣的副作用,具体取决于您的应用程序和 Web 服务器。在您已经通过打印或回显发送输出后,您可能能够发送标头和 cookie。如果您的服务器由于缓冲输出而发出的数据包较少,而 PHP 在获得输出时对输出进行流式传输,您也可能会看到性能优势。在生产服务器上,出于性能原因,4096 字节是一个很好的设置。
注意:输出缓冲也可以通过输出缓冲控制功能来控制。
可能的值:
On = 启用且缓冲区不受限制。(谨慎使用)
关闭 = 禁用
Integer = 启用缓冲区并设置其最大大小(以字节为单位)。
注意:对于 CLI SAPI,此指令硬编码为 Off
默认值:关闭
开发价值:4096
产值:4096
http://php.net/output-buffering output_buffering=4096