PHP 缓冲区为什么 [英] PHP buffer why

查看:18
本文介绍了PHP 缓冲区为什么 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在评论中,我有一些关于以下脚本的概念性问题(我认为都是相关的).脚本运行良好.

I have a few conceptual questions (all related, I think) regarding the following script, at the comments. The script works fine.

<?PHP
ob_start();

// Create string to overflow browser buffer ...?
$buffer = str_repeat(" ", 4096);

// Indicate new header / html content ...?
$buffer .= "
<span></span>
";

for ($i=0; $i<5; $i++) {
  echo $buffer.$i;
  ob_flush();
  flush();
  sleep(1);
}

ob_end_flush();
?>

首先,为什么我需要将 <tag> 发送到浏览器?我认为它与标题有关.

First, why do I need to send the <tag> to the browser? I assume it has something to do with headers.

第二,为什么中间需要一些HTML?

Second, why do I need some HTML in the middle?

第三,使用 256 字节而不是 4096 的示例有很多.但是,如果我使用 256,脚本将不起作用.这些示例是否已过时,将来这个数字会再次更改吗?

Third, there are many examples that use 256 bytes instead of 4096. However, the script doesn't work if I use 256. Are these examples outdated, and will this number change again in the future?

//编辑源链接

此代码主要来自 php.net 中的 注释 sleep() 函数这个 SO 的解决方案问题.都没有提到为什么要包含 .

This code was gathered mainly from the commentary in php.net sleep() function and the solution to this SO question. Neither mentions why to include .

//编辑标题

如果我不添加 、HTML 标记和第二组 ,脚本将无法在 Chrome 中正确执行或 Safari(它只是一次转储所有值).

If I don't add , an HTML tag, and a second set of , the script will not execute properly in Chrome or Safari (it just dumps all the values at once).

此外,如果在 session_start() 之前调用它,则会引发错误:无法发送会话缓存限制器 - 标头已发送".

Additionally, if this is called before a session_start(), it throws an error: "Cannot send session cache limiter - headers already sent".

推荐答案

首先,为什么我需要将 <tag> 发送到浏览器?我认为它与标题有关.

First, why do I need to send the <tag> to the browser? I assume it has something to do with headers.

第二,为什么中间需要一些HTML?

Second, why do I need some HTML in the middle?

通常浏览器必须等到他们获取了整个响应,直到它可以被呈现(想想在最后一个字符之前可以有效的 XML).但由于这会带来糟糕的用户体验,因此大多数浏览器会尽早开始解析和呈现内容.

Normally browser have to wait until they have fetched the whole response until it can be rendered (just think of XML that can be valid until the last character). But since that would make a bad user experience, most browsers start to parse and render the contents as early as possible.

这里这个 HTML 片段可能是浏览器实际构建 DOM 并开始渲染的发起者.

And here this HTML fragment could be the initiator for the browser to actually build the DOM and start rendering.

第三,使用 256 字节而不是 4096 的示例有很多.但是,如果我使用 256,脚本将不起作用.这些示例是否已过时,将来这个数字会再次更改吗?

Third, there are many examples that use 256 bytes instead of 4096. However, the script doesn't work if I use 256. Are these examples outdated, and will this number change again in the future?

由于手册提示 Web 服务器中可能包含一些进一步的缓冲,这可能是试图溢出这些缓冲区,它们也被刷新以达到预期的效果.

As the manual hints that there might be some further buffering incorporated in the web server, this might be the attempt to overflow those buffers that they are also flushed in order to have the expected effect.

这篇关于PHP 缓冲区为什么 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆