PHP循环;如何打印每个结果并在回显另一个结果之前延迟一秒钟? [英] PHP loop; how to print each result and delay it for a second before echoing another result?

查看:13
本文介绍了PHP循环;如何打印每个结果并在回显另一个结果之前延迟一秒钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 stackoverflow 上的第一个问题,我只是好奇.. 是否可以在 PHP 中延迟循环?我正在尝试将每个结果打印到浏览器并在处理另一个循环之前使用 sleep() 暂停脚本,但它不起作用,这是我使用的脚本:

this is my first question here on stackoverflow, I just curious.. is it possible to delay loop in PHP ? I'm trying to print each result to browser and pause the script using sleep() before it process another loop, but it's not working, here's the script that I use:

<?php
$n = 1;

while ($n < 10) {
    echo $n."<br />";
    $n++;
    sleep(1);
}
?>

PS:我在 Linux Mint 上使用 Firefox 和 Apache2.

PS: I'm using Firefox and Apache2 on Linux Mint.

推荐答案

服务器通常会缓冲服务器端脚本的输出,直到其中有足够的内容可以输出尝试这样的事情.设置输出缓冲关闭和手动刷新缓冲区的组合.请注意隐式冲洗线以及冲洗和 ob_flush 线.

Servers usually buffer the output of a server side script until there's enough in it to output try something like this. Combination of setting output buffering off and manually flushing the buffer. Note the implcit flush line and the flush and ob_flush lines.

<?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
";
for ($i = 0; $i < 1150; $i++) {
    echo $i." -- ";

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

?>

这篇关于PHP循环;如何打印每个结果并在回显另一个结果之前延迟一秒钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆