立即打印结果(php)

前端之家收集整理的这篇文章主要介绍了立即打印结果(php)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 PHP脚本连接10个不同的服务器来获取数据.我想要在第二个开始之前打印第一个连接的结果.
使用 flush和/或 ob_flush,你应该得到你想要的.

这是一个快速演示:

  1. for ($i=0 ; $i<10 ; $i++) {
  2. echo "$i<br />";
  3. ob_flush();
  4. flush();
  5. sleep(1);
  6. }

每秒钟,一个数字将被发送到浏览器,而不用等待循环/脚本结束.
(没有flush和ob_flush,它等待直到脚本结束发送输出)

说明你为什么需要这两个,从手册中的flush页引用:

Flushes the write buffers of PHP and
whatever backend PHP is using (CGI,a
web server,etc). This attempts to
push current output all the way to the
browser with a few caveats.

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.

如果这不适合您,请查看手册两页上的评论,可以给出几点“为什么会失败”的指针

猜你在找的PHP相关文章