php 在每次长循环迭代时回显“字符串”(flush() 不起作用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6556790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Echo 'string' while every long loop iteration (flush() not working)
提问by Nyxynyxx
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?
在整个脚本完成之前,这不会显示回声。什么地方出了错?
回答by Gregory Burns
From PHP Manual:
来自 PHP 手册:
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.
flush() 可能无法覆盖您的 Web 服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲都没有影响。它也不影响 PHP 的用户空间输出缓冲机制。这意味着如果您正在使用它们,您将必须同时调用 ob_flush() 和 flush() 来刷新 ob 输出缓冲区。
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";
}
?>
回答by Grigoreas P.
try this
尝试这个
while (@ob_end_flush());
ob_implicit_flush(true);
echo "first line visible to the browser";
echo str_pad("",1024," ");
echo "<br />";
sleep(5);
echo "second line visible to the browser after 5 secs";
Just notice that this way you're actually disabling the output buffer for your current script. So if you're trying to 'ob_end_flush()' after that you'll get a warning that there is no buffer to close.
请注意,这样您实际上是在禁用当前脚本的输出缓冲区。因此,如果您在此之后尝试 'ob_end_flush()',您将收到一条警告,提示没有缓冲区可以关闭。
回答by radoo
Make sure you first do:
确保您首先执行以下操作:
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
@ob_end_clean();
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
@ob_end_clean();
and then just flush();
every time you need to output your echo'es to the browser.
然后flush();
每次您需要将您的 echo'es 输出到浏览器时。
回答by Amir Fo
Add this on the headerof the script:
将此添加到脚本的标题中:
ob_start();
ob_implicit_flush();
Implicit flushing will result in a flush operation after every output call so that explicit calls to flush() will no longer be needed. Please note that adding implicit flushing in the script execution affects on performance. You can add a debugging mode for your script like:
隐式刷新将在每次输出调用后导致刷新操作,因此不再需要显式调用 flush()。请注意,在脚本执行中添加隐式刷新会影响性能。您可以为脚本添加调试模式,例如:
ob_start();
define(DEBUG, 1);
if(DEBUG){
ob_implicit_flush();
}
回答by SteAp
In general, the desired behavior isn't possible is a deterministic/stable wayusing pure PHP and HTML.
通常,使用纯 PHP 和 HTML的确定性/稳定方式不可能实现所需的行为。
If and how a browser renders a partial page depends on the browser, proxies and caches. Thus, even if the stuff works on your test machine, it's likely, that it does not on your production system.
浏览器是否以及如何呈现部分页面取决于浏览器、代理和缓存。因此,即使这些东西在您的测试机器上工作,也很可能在您的生产系统上不起作用。
The library xAjaxprovides a well-integrated solution to manage AJAX style updates with PHP. While xAjax might be dead as a project (at least right now), it still works fine.
库xAjax提供了一个集成良好的解决方案,用于使用 PHP 管理 AJAX 样式更新。虽然 xAjax 作为一个项目可能已经死了(至少现在是这样),但它仍然可以正常工作。
回答by Julien Ducro
You could try to also use ob_flush() sometimes both are needed to work.
您也可以尝试使用 ob_flush() 有时两者都需要工作。
回答by krom
Also make sure to output a Content-type header first. Flushing doesn't work for me without:
还要确保首先输出一个 Content-type 标头。没有以下情况,冲洗对我不起作用:
header( 'Content-type: text/html; charset=utf-8' );
for($i=0; $i<10; ++$i) {
echo "Loop<br />\n";
ob_flush();
flush();
sleep(1);
}