<?php
ob_start();    //-----------------------------1//for($s=0;$s<=300;$s++){echo '';}//ob_end_clean();  //----------------------------------2for($var=0;$var<=10;$var++){
echo $var;
print str_repeat("", 4096); 
ob_flush();//--------------------------3
flush();  //--------------------------4
   sleep(1); //--------------------------5
}
?>
一个测试缓冲区的例子,问题来了,当使用ob_start()函数时候,【3】的作用是释放缓冲区的内容,【4】将不再缓冲区的内容发送到页面,【5】使得每秒发送一次,但是为什么还会等10秒后一次将内容全发送过去.查了很多文章,很多都说使用【2】处的方法,注释掉【1】【3】,但是这样就关闭了缓冲区,网上关于Php的说法有称内部缓冲区的,也有叫缓冲区的,不知道是不是一个东西。
还有Php.ini文件中的 output_buffering他的缓冲区是不是就是ob_start()开启的缓冲区?

解决方案 »

  1.   

    ob_flush不是很好用,请参考官方文档。
    http://il.php.net/manual/en/function.ob-flush.phpsome problems with ob_flush() and flush() could be resolved by defining content type header :
    header( 'Content-type: text/html; charset=utf-8' );so working code looks like this:
    <?php
    header( 'Content-type: text/html; charset=utf-8' );
    echo 'Begin ...<br />';
    for( $i = 0 ; $i < 10 ; $i++ )
    {
        echo $i . '<br />';
        flush();
        ob_flush();
        sleep(1);
    }
    echo 'End ...<br />';
    ?>