PHP有while和sleep的问题,while循环只在循环结束时回显所有


php issue with while and sleep , the while loop echo all only at the end of the loop

我有一个php while和sleep的问题,while循环只在循环结束时返回,而不是每1秒返回一次

我试过了:

<?php
ob_implicit_flush(true);
ob_end_flush();

$i=0;
while($i<=5) {
     echo $i;
     sleep(1);
     flush();
     $i++;
}
?>

试试这个。它在我的浏览器

<?php
ini_set('zlib.output_compression','Off');// if not in php.ini
header( 'Content-type: text/html; charset=utf-8' );
header("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
ob_implicit_flush(true);
ob_end_flush();
$i=0;
while($i<=5) {
     echo $i;
     flush();
     sleep(1);
     $i++;
}
?>

在php.ini zlib.output_compression=Off

中确保此值

也可以参考这个问题的所有答案获得详细的答案

if (!ob_get_level()) {
    ob_start();
} else {
    ob_end_clean();
    ob_start();
}
$i = 0;
while ($i <= 5) {
    flush();
    ob_flush();
    echo $i . "'n";
    $i++;
    sleep(1);
}

这段代码将从刷新所有的echo开始,然后开始一个接一个地输出echo