search
Homephp教程php手册php利用缓冲实现动态输出(flush,ob_flush)

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

Jun 06, 2016 am 09:52 AM
phpcodedynamicaccomplishOpen sourceprogrammingprogramming languagesoftware developmentoutput

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";
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools