本文介绍了在每个长循环迭代时回显“字符串"(flush()不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行周期很长的循环,并且我希望脚本在每次循环迭代完成时都显示一些内容.

I have a loop that takes very long to execute, and I want the script to display something whenever the loop iteration is done.

echo "Hello!";

flush();

for($i = 0; $i < 10; $i ++) {
    echo $i;
    //5-10 sec execution time
    flush();
}

在整个脚本完成之前,它不会显示回显.出了什么问题?

This does not display the echos until the entire script is completed. What went wrong?

推荐答案

来自PHP手册:

flush()可能无法覆盖Web服务器的缓冲方案,并且对浏览器中的任何客户端缓冲都没有影响.它也不会影响PHP的用户空间输出缓冲机制.这意味着,如果您正在使用ob_flush()和flush()来刷新ob输出缓冲区,则必须同时调用它们.

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

echo "Hello!";
flush();
ob_flush();

for($i = 0; $i < 10; $i ++) {
    echo $i;
    //5-10 sec execution time
    flush();
    ob_flush();
}

-或者-您可以刷新并关闭缓冲

-or- you can flush and turn off Buffering

<?php
//Flush (send) the output buffer and turn off output buffering
while (ob_get_level() > 0)
    ob_end_flush();

echo "Hello!";

for($i = 0; $i < 10; $i ++) {
    echo $i . "\r\n";
}

?>

这篇关于在每个长循环迭代时回显“字符串"(flush()不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:00
查看更多