为什么需要`str_pad('',4096)`才能使PHP刷新正常工作? [英] Why do I need `str_pad('',4096)` to make PHP flushing work?
问题描述
例如,
这不起作用(Firefox 21,IE8):
this doesn't work (Firefox 21, IE8):
<?php
function flush_buffers(){
ob_end_flush();
ob_flush();
flush();
ob_start();
}
ob_start();
echo 'Text 1<br />';
flush_buffers();
Sleep(2);
echo 'Text 2<br />';
flush_buffers();
Sleep(2);
echo 'Text 3<br />';
flush_buffers();
Sleep(2);
echo 'Text 4<br />';
?>
但这一项有效:
<?php
function flush_buffers(){
echo str_pad('',4096);
ob_end_flush();
ob_flush();
flush();
ob_start();
}
ob_start();
echo 'Text 1<br />';
flush_buffers();
Sleep(2);
echo 'Text 2<br />';
flush_buffers();
Sleep(2);
echo 'Text 3<br />';
flush_buffers();
Sleep(2);
echo 'Text 4<br />';
?>
我有在Win XP SP3上运行的PHP 5.4.11 VC9和Apache 2.4.3(apacheLounge).
I have PHP 5.4.11 VC9 and Apache 2.4.3 (apacheLounge) running on Win XP SP3.
推荐答案
某些浏览器包括其自己的内部缓冲区,以便以更少的断断续续下载和显示效率.在大多数情况下,此缓冲区为4Kb或4096字节.
Some browsers include their own internal buffer in order to download and display more efficiently with less choppiness. In most cases, this buffer is 4Kb, or 4096 bytes.
str_pad('',4096)
的作用是在输出中写入4,096个空格.由于是HTML,因此这些空间会合并为一个空间.
What str_pad('',4096)
does is write 4,096 spaces to the output. Since it's HTML, these spaces collapse into a single space.
总体而言,不应依赖此行为.浏览器用于查看网页,而不是混入控制台终端.
Overall, this behaviour should NOT be relied upon. Browsers are for viewing webpages, not bastardising into console terminals.
此外,为什么还要写</br>
?没有结尾<br>
标签,并且自动关闭的版本是<br />
Also, why are you writing </br>
? There is no such thing as an end <br>
tag, and the self-closing version is <br />
这篇关于为什么需要`str_pad('',4096)`才能使PHP刷新正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!