php利用緩沖實現動態輸出通過 flush,ob_flush實現
print str_repeat(" ", 4096);//php.ini output_buffering默認是4069字符或者更大,即輸出內容必須達到4069字符服務器才會flush刷新輸出緩沖 for ($i=10; $i>0; $i--) { echo $i; ob_flush(); flush(); sleep(1); } //ob_flush()和flush()的區別。前者是把數據從PHP的緩沖中釋放出來,后者是把不在緩沖中的或者說是被釋放出來的數據發送到瀏覽器。所以當緩沖存在的時候,我們必須ob_flush()和flush()同時使用。
//附上一段非常有趣的代碼,作者為PuTTYshell。在一個腳本周期里,每次輸出,都會把前一次的輸出覆蓋掉。 header('Content-type: multipart/x-mixed-replace;boundary=endofsection'); print "\n--endofsection\n"; $pmt = array("-", "\\", "|", "/" ); for( $i = 0; $i <10; $i ++ ){ sleep(1); print "Content-type: text/plain\n\n"; print "Part $i\t".$pmt[$i % 4]; print "--endofsection\n"; ob_flush(); flush(); } print "Content-type: text/plain\n\n"; print "The end\n"; print "--endofsection--\n";