>  기사  >  php教程  >  php利用缓冲实现动态输出(flush,ob_flush)

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

WBOY
WBOY원래의
2016-06-06 09:52:291560검색

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";
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.