php利用缓冲实现动态输出通过 flush,ob_flush实现
<span style="color: #0000ff;">print</span> <span style="color: #008080;">str_repeat</span>(" ", 4096);<span style="color: #008000;">//</span><span style="color: #008000;">php.ini output_buffering默认是4069字符或者更大,即输出内容必须达到4069字符服务器才会flush刷新输出缓冲</span> <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=10; <span style="color: #800080;">$i</span>>0; <span style="color: #800080;">$i</span>--<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$i</span><span style="color: #000000;">; </span><span style="color: #008080;">ob_flush</span><span style="color: #000000;">(); </span><span style="color: #008080;">flush</span><span style="color: #000000;">(); </span><span style="color: #008080;">sleep</span>(1<span style="color: #000000;">); } </span><span style="color: #008000;">//</span><span style="color: #008000;">ob_flush()和flush()的区别。前者是把数据从PHP的缓冲中释放出来,后者是把不在缓冲中的或者说是被释放出来的数据发送到浏览器。所以当缓冲存在的时候,我们必须ob_flush()和flush()同时使用。</span>
<span style="color: #008000;">//</span><span style="color: #008000;">附上一段非常有趣的代码,作者为PuTTYshell。在一个脚本周期里,每次输出,都会把前一次的输出覆盖掉。</span> <span style="color: #008080;">header</span>('Content-type: multipart/x-mixed-replace;boundary=endofsection'<span style="color: #000000;">); </span><span style="color: #0000ff;">print</span> "\n--endofsection\n"<span style="color: #000000;">; </span><span style="color: #800080;">$pmt</span> = <span style="color: #0000ff;">array</span>("-", "\\", "|", "/"<span style="color: #000000;"> ); </span><span style="color: #0000ff;">for</span>( <span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $i ++<span style="color: #000000;"> ){ </span><span style="color: #008080;">sleep</span>(1<span style="color: #000000;">); </span><span style="color: #0000ff;">print</span> "Content-type: text/plain\n\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">print</span> "Part <span style="color: #800080;">$i</span>\t".<span style="color: #800080;">$pmt</span>[<span style="color: #800080;">$i</span> % 4<span style="color: #000000;">]; </span><span style="color: #0000ff;">print</span> "--endofsection\n"<span style="color: #000000;">; </span><span style="color: #008080;">ob_flush</span><span style="color: #000000;">(); </span><span style="color: #008080;">flush</span><span style="color: #000000;">(); } </span><span style="color: #0000ff;">print</span> "Content-type: text/plain\n\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">print</span> "The end\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">print</span> "--endofsection--\n";