ax.*_*ax. 267
如果没有输出缓冲(默认),您的HTML将通过脚本在PHP处理过程中分批发送到浏览器.使用输出缓冲,您的HTML存储在变量中,并在脚本末尾作为一个整体发送到浏览器.
Web开发人员的输出缓冲优势
- 单独启用输出缓冲会减少下载和呈现HTML所需的时间,因为当PHP处理HTML时,它不会被分批发送到浏览器.
- 我们可以用PHP字符串做的所有花哨的东西,我们现在可以将整个HTML页面作为一个变量.
- 如果您在设置cookie时遇到过"警告:无法修改标题信息 - 已经由(输出)发送的标题"的消息,您将很高兴知道输出缓冲是您的答案.
Sal*_*n A 68
PHP使用输出缓冲来提高性能并执行一些技巧.
您可以让PHP将所有输出存储到缓冲区中,并立即输出所有输出,从而提高网络性能.
在某些情况下,您可以访问缓冲区内容而不将其发送回浏览器.
考虑这个例子:
<?php
ob_start( );
phpinfo( );
$output = ob_get_clean( );
?>
Run Code Online (Sandbox Code Playgroud)
上面的示例将输出捕获到变量中,而不是将其发送到浏览器.默认情况下,output_buffering处于关闭状态.
考虑这个例子:
<?php
ob_start( );
echo "Hello World";
if ( $some_error )
{
header( "Location: error.php" );
exit( 0 );
}
?>
Run Code Online (Sandbox Code Playgroud)
Jul*_*ian 12
我知道这是一个老问题,但我想写一些视觉学习者的答案.我无法在全球网络上找到解释输出缓冲的任何图表,所以我自己在mspaint中制作了一些东西.
如果关闭输出缓冲,mspaint.exe则会立即将数据发送到浏览器.
如果打开输出缓冲,则会在echo将数据发送到浏览器之前将数据发送到输出缓冲区.
的phpinfo
要查看输出缓冲是否打开/关闭,请参阅核心部分的phpinfo.该echo指令将告诉您输出缓冲是否打开/关闭.
在这种情况下,output_buffering值为4096,这意味着缓冲区大小为4 KB.它还意味着在Web服务器上打开输出缓冲.
php.ini中
可以通过更改output_buffering指令的值来打开/关闭并更改缓冲区大小.只需找到它output_buffering,将其更改为您选择的设置,然后重新启动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
Run Code Online (Sandbox Code Playgroud)
该指令php.ini不是关于输出缓冲的唯一可配置指令.您可以在此处找到其他可配置的输出缓冲指令:http://php.net/manual/en/outcontrol.configuration.php
示例:ob_get_clean()
您可以在下面看到如何output_buffering在将其发送到浏览器之前捕获并操作它.
// 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
Run Code Online (Sandbox Code Playgroud)
示例:Hackingwithphp.com
有关带有示例的输出缓冲区的更多信息,请访问:
http://www.hackingwithphp.com/13/0/0/output-buffering
输出控制功能允许您控制何时从脚本发送输出.这在几种不同的情况下非常有用,特别是如果您需要在脚本开始输出数据后将标头发送到浏览器.输出控制功能不会影响使用header()或setcookie()发送的标头,只会影响诸如echo()之类的函数以及PHP代码块之间的数据.
http://php.net/manual/en/book.outcontrol.php
更多资源:
| 归档时间: |
|
| 查看次数: |
147999 次 |
| 最近记录: |