php flush类输出缓冲剖析

2019-04-10 15:27:54于丽

{
echo $i;
ob_flush();
sleep(1);
}
?>
<?php
ob_end_clean();
ob_implicit_flush(true);
for ($i=10; $i>0; $i--)
{
echo $i;
sleep(1);
}
?>
请注意看上面的ob_implicit_flush(true),这个函数强制每当有输出的时候,即刻把输出发送到浏览器。这样就不需要每次输出(echo)后,都用flush()来发送到浏览器了。
以上所诉可能在某些浏览器中不成立。因为浏览器也有自己的规则。我是用Firefox1.5,IE6,opera8.5来测试的。其中opera就不能正常输出,因为它有一个规则,就是不遇到一个HTML标签,就绝对不输出,除非到脚本结束。而FireFox和IE还算比较正常的。
最后附上一段非常有趣的代码,作者为PuTTYshell。在一个脚本周期里,每次输出,都会把前一次的输出覆盖掉。
以下代码只在firefox下可用,其他浏览器并不支持multipart/x-mixed-replace的Content-Type.
<?php
header('Content-type: multipart/x-mixed-replace;boundary=endofsection');
print "n--endofsectionn";
$pmt = array("-", "", "|", "/" );
for( $i = 0; $i <10; $i ++ ){
sleep(1);
print "Content-type: text/plainnn";
print "Part $it".$pmt[$i % 4];
print "--endofsectionn";
ob_flush();
flush();
}
print "Content-type: text/plainnn";
print "The endn";
print "--endofsection--n";
?>

相关文章 大家在看