调用echo后,PHP刷新输出 [英] PHP flushing output as soon as you call echo
问题描述
我认为flush();
可以工作,至少从Google/Stackoverflow告诉我的内容来看,但是在我的Windows WAMP(Windows,Apache,MySQL,PHP)系统上,这是行不通的.
是否需要设置一些PHP设置才能使flush()工作?
这是我的代码:
<?php
echo "Fun";
flush();
sleep(5);
echo "<br>Mo";
?>
当脚本执行完毕(5秒后)时,代码将全部输出在一起.我不希望这样,我想立即显示"Fun",然后在5秒钟后显示"Mo". /p>
我尝试了其他刷新组合,例如ob_end_flush();
or ob_implicit_flush(true);
,但没有任何效果.有什么想法吗?
脚本在CLI上运行良好,显示"Fun",等待5秒钟,然后显示< br> Mo".
对于浏览器,结果可能会有所不同,因为:
- 浏览器不会立即开始渲染.为HTML文档获取3个字节的数据还不足以做任何事情,因此很可能还要再等几个.
- lib级别的隐式IO缓冲很可能一直处于活动状态,直到收到换行符为止.
要解决此问题,请执行以下操作:1)使用文本/纯文本内容类型进行测试; 2)需要换行符,因此echo "Fun\n";
和echo "<br>Mo\n";
当然也不会对真实的HTML数据使用text/plain.
I thought flush();
would work, at least from what Google/Stackoverflow tell me, but on my Windows WAMP (Windows, Apache, MySQL, PHP) system it doesn't work.
Is there some PHP setting I have to set to make flush() work?
Here's my code:
<?php
echo "Fun";
flush();
sleep(5);
echo "<br>Mo";
?>
The code just outputs all together when the script is done executing (after 5 seconds).. I don't want this, I want 'Fun' to show up right away, and then after 5 seconds 'Mo'.
I've tried other combinations of flush like ob_end_flush();
or ob_implicit_flush(true);
but nothing is working. Any ideas?
The script works fine from CLI, displaying "Fun", waiting 5 secs before displaying "<br>Mo".
For a browser the results might be a bit different because:
- The browser wont start rendering right away. Getting 3 bytes of data for HTML document isn't enough to do anything, so it'll most likely wait for a few more.
- Implicit IO buffering on the lib level will most likely be active until a newline is received.
To work around 1) use text/plain content type for your test; 2) needs newlines, so do an echo "Fun\n";
and echo "<br>Mo\n";
Of course you wont be using text/plain for real HTML data.
这篇关于调用echo后,PHP刷新输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!