【问题标题】:Why does PHP wait to print为什么 PHP 等待打印
【发布时间】:2017-05-31 15:55:18
【问题描述】:

这是我的代码:

<?php
for($j = 0; $j < 10; $j++){
    print((string)$j . " ");
    sleep(1);
}
?>

代码等待打印

0 1 2 3 4 5 6 7 8 9

直到 10 秒后,而不是每秒打印一个。

为什么会这样,我该如何解决?

【问题讨论】:

标签: php


【解决方案1】:

服务器通常会缓冲服务器端脚本的输出,直到其中有足够的输出尝试这样的事情。设置输出缓冲关闭和手动刷新缓冲区的组合。注意 implcit flush 行以及 flush 和 ob_flush 行。

<?php 
@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('max_execution_time',1200);


header( 'Content-type: text/html; charset=utf-8' );


echo "Testing time out in seconds\n";
for ($i = 0; $i < 10; $i++) {
    echo $i." -- ";

    if(sleep(1)!=0)
    {
        echo "sleep failed script terminating"; 
        break;
    }
    flush();
    ob_flush();
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2022-01-21
    • 2021-05-29
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多