Home > php教程 > php手册 > body text

php利用缓冲实现动态输出(flush,ob_flush)

WBOY
Release: 2016-06-06 09:52:29
Original
1558 people have browsed it

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>
Copy after login
<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";
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!