php 在脚本仍在执行时显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5415665/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:20:04  来源:igfitidea点击:

Show results while script is still executing

php

提问by adam

Right now in order to see the results, I have to wait until the entire code is done executing. It hangs until it's complete and stays loading. Once it's finished it shows all the information I was looking for.. Is there anyway to show this while the script is still running? So say if I have a print somewhere at the top of my code, I want it to show when it's called not when the script is done executing.

现在为了查看结果,我必须等到整个代码执行完毕。它挂起直到它完成并保持加载。完成后,它会显示我正在寻找的所有信息.. 无论如何在脚本仍在运行时显示它?因此,如果我在代码顶部的某处有一个打印,我希望它显示它何时被调用而不是在脚本执行完成时显示。

Anyone know how to do this?

有人知道怎么做吗?

Thanks

谢谢

回答by Rob Agar

You can use output bufferinglike this:

您可以像这样使用输出缓冲

ob_start();

echo('doing something...');

// send to browser
ob_flush();

// ... do long running stuff
echo('still going...');

ob_flush();

echo('done.');
ob_end_flush(); 

回答by Claudio

This one worked for me: (source)

这个对我有用:(来源

function output($str) {
    echo $str;
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}

回答by fab

You can do that with output buffering. Turn on output buffering at the top of your script with ob_start(). That makes PHP to send no output to the browser. Instead its stored internally. Flush your output at any time with ob_flush(), and the content will be sent to the browser.
But keep in mind that output buffering is influenced by many other factors. I think some versions of IIS will wait until the script is finished, ignoring output buffering. And some Antivirus software on client side (Was it Panda?) might wait until the page is fully loaded before passing it through to the browser.

您可以使用输出缓冲来做到这一点。使用 . 打开脚本顶部的输出缓冲ob_start()。这使得 PHP 不向浏览器发送任何输出。相反,它存储在内部。随时使用 刷新您的输出,ob_flush()内容将发送到浏览器。
但请记住,输出缓冲受许多其他因素的影响。我认为某些版本的 IIS 会等到脚本完成,而忽略输出缓冲。客户端的一些防病毒软件(它是Panda吗?)可能会等到页面完全加载后再将其传递给浏览器。

回答by agold

I had to put both ob-flushand flushas shown in this example:

我不得不把两者都ob-flush加上flush,如本例所示:

for($i=10; $i > 0; $i--)
{
    echo "$i ...";
    flush();
    ob_flush();
    sleep(1);
}