php 什么是输出缓冲?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2832010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:48:22  来源:igfitidea点击:

What is output buffering?

phpoutput-buffering

提问by Abhimanyu

What is output buffering and why is one using it in PHP?

什么是输出缓冲?为什么要在 PHP 中使用它?

回答by ax.

Output Buffering for Web Developers, a Beginner's Guide:

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.

如果没有输出缓冲(默认设置),您的 HTML 将在 PHP 通过脚本处理时分片发送到浏览器。通过输出缓冲,您的 HTML 存储在一个变量中,并在脚本末尾作为一个整体发送到浏览器。

输出缓冲对 Web 开发人员的优势

  • 单独打开输出缓冲可以减少下载和呈现 HTML 所需的时间,因为它不会在 PHP 处理 HTML 时分片发送到浏览器。
  • 我们可以用 PHP 字符串做的所有花哨的事情,我们现在可以将整个 HTML 页面作为一个变量来做。
  • 如果您在设置 cookie 时遇到过“警告:无法修改标头信息 - 标头已由(输出)发送”的消息,您会很高兴知道输出缓冲是您的答案。

回答by Salman A

Output bufferingis used by PHP to improve performance and to perform a few tricks.

PHP 使用输出缓冲来提高性能并执行一些技巧。

  • You can have PHP store all output into a buffer and output all of it at once improving network performance.

  • You can access the buffer content without sending it back to browser in certain situations.

  • 您可以让 PHP 将所有输出存储到一个缓冲区中并一次输出所有输出,从而提高网络性能。

  • 在某些情况下,您可以访问缓冲区内容而无需将其发送回浏览器。

Consider this example:

考虑这个例子:

<?php
    ob_start( );
    phpinfo( );
    $output = ob_get_clean( );
?>

The above example captures the output into a variable instead of sending it to the browser. output_buffering is turned off by default.

上面的示例将输出捕获到变量中,而不是将其发送到浏览器。默认情况下,输出缓冲是关闭的。

  • You can use output buffering in situations when you want to modify headers after sending content.
  • 在发送内容后要修改标头的情况下,您可以使用输出缓冲。

Consider this example:

考虑这个例子:

<?php
    ob_start( );
    echo "Hello World";
    if ( $some_error )
    {
        header( "Location: error.php" );
        exit( 0 );
    }
?>

回答by Julian

I know that this is an old question but I wanted to write my answer for visual learners. I couldn't find any diagrams explaining output buffering on the worldwide-web so I made a diagram myself in Windows mspaint.exe.

我知道这是一个老问题,但我想为视觉学习者写下我的答案。我在万维网上找不到任何解释输出缓冲的图表,所以我自己在 Windows 中制作了一个图表mspaint.exe

If output buffering is turned off, then echowill send data immediately to the Browser.

如果输出缓冲关闭,则将echo立即将数据发送到浏览器。

enter image description here

在此处输入图片说明

If output buffering is turned on, then an echowill send data to the output buffer before sending it to the Browser.

如果打开了输出缓冲,则在echo将数据发送到浏览器之前,会将数据发送到输出缓冲区。

enter image description here

在此处输入图片说明

phpinfo

php信息

To see whether Output buffering is turned on / off please refer to phpinfo at the core section. The output_bufferingdirective will tell you if Output buffering is on/off.

要查看输出缓冲是否打开/关闭,请参阅核心部分的 phpinfo。该output_buffering指令将告诉您输出缓冲是否打开/关闭。

enter image description hereIn this case the output_bufferingvalue is 4096 which means that the buffer size is 4 KB. It also means that Output buffering is turned on, on the Web server.

在此处输入图片说明在这种情况下,output_buffering值为 4096,这意味着缓冲区大小为 4 KB。这也意味着在 Web 服务器上打开了输出缓冲。

php.ini

配置文件

It's possible to turn on/off and change buffer size by changing the value of the output_bufferingdirective. Just find it in php.ini, change it to the setting of your choice, and restart the Web server. You can find a sample of my php.inibelow.

可以通过更改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

The directive output_bufferingis not the only configurable directive regarding Output buffering. You can find other configurable Output buffering directives here: http://php.net/manual/en/outcontrol.configuration.php

该指令output_buffering不是关于输出缓冲的唯一可配置指令。您可以在此处找到其他可配置的输出缓冲指令:http: //php.net/manual/en/outcontrol.configuration.php

Example: ob_get_clean()

示例:ob_get_clean()

Below you can see how to capture an echoand manipulate it before sending it to the browser.

下面你可以看到如何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

Examples: Hackingwithphp.com

示例:Hackingwithphp.com

More info about Output buffer with examples can be found here:

可以在此处找到有关带有示例的输出缓冲区的更多信息:

http://www.hackingwithphp.com/13/0/0/output-buffering

http://www.hackingwithphp.com/13/0/0/output-buffering

回答by Sarfraz

The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo() and data between blocks of PHP code.

输出控制功能允许您控制何时从脚本发送输出。这在几种不同的情况下都很有用,尤其是在脚本开始输出数据后需要向浏览器发送标头时。输出控制函数不影响使用 header() 或 setcookie() 发送的标头,只影响诸如 echo() 和 PHP 代码块之间的数据之类的函数。

http://php.net/manual/en/book.outcontrol.php

http://php.net/manual/en/book.outcontrol.php

More Resources:

更多资源:

Output Buffering With PHP

使用 PHP 缓冲输出

回答by nik

As name suggest here memory buffer used to manage how the output of script appears.

顾名思义,这里的内存缓冲区用于管理脚本输出的显示方式。

Here is one very good tutorialfor the topic

这是该主题的一个非常好的教程

回答by sudip

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

Buffers can be nested, so while one buffer is active, another ob_start()activates a new buffer. So ob_end_flush()and ob_flush()are not really sending the buffer to the output, but to the parent buffer. And only when there is no parent buffer, contents is sent to browser or terminal.

缓冲区可以嵌套,因此当一个缓冲区处于活动状态时,另一个会ob_start()激活一个新缓冲区。所以ob_end_flush()ob_flush()没有真正将缓冲区发送到输出,而是发送到父缓冲区。并且只有当没有父缓冲区时,内容才会发送到浏览器或终端。

Nicely explained here: https://phpfashion.com/everything-about-output-buffering-in-php

在这里很好地解释:https: //phpfashion.com/everything-about-output-buffering-in-php

回答by Kamil D?browski

UPDATE 2019. If you have dedicated server and SSD or better NVM, 3.5GHZ. You shouldn't use buffering to make faster loaded website in 100ms-150ms.

2019 年更新。如果您有专用服务器和 SSD 或更好的 NVM,3.5GHZ。您不应该使用缓冲在 100 毫秒到 150 毫秒内使网站加载速度更快。

Becouse network is slowly than proccesing script in the 2019 with performance servers (severs,memory,disk) and with turn on APC PHP :) To generated script sometimes need only 70ms another time is only network takes time, from 10ms up to 150ms from located user-server.

因为网络比在 2019 年使用性能服务器(服务器、内存、磁盘)和打开 APC PHP 处理脚本慢:) 生成脚本有时只需要 70 毫秒,而另一种时间只是网络需要时间,从定位到 10 毫秒到 150 毫秒用户服务器。

so if you want be fast 150ms, buffering make slowl, becouse need extra collection buffer data it make extra cost. 10 years ago when server make 1s script, it was usefull.

所以如果你想要快 150 毫秒,缓冲会变慢,因为需要额外的收集缓冲区数据,这会增加额外的成本。10 年前当服务器生成 1s 脚本时,它很有用。

Please becareful output_buffering have limit if you would like using jpg to loading it can flush automate and crash sending.

如果您想使用 jpg 加载它可以刷新自动和崩溃发送,请注意 output_buffering 有限制。

Cheers.

干杯。

You can make fast river or You can make safely tama :)

您可以制作快速河流或您可以安全制作 tama :)