php立即输出缓存中的数据

比如我写这么一个程序

<?php
// filename:echo.php
echo "this is the first line <br />";
sleep(2);
echo "this is the second line <br />";
sleep(3);
echo "this is the third line <br />";
?>

如果我使用apache作为web服务器,然后使用浏览器浏览的话,肯定是6秒后一起输出三行。

但是,如果我希望它们能够在执行的过程中顺次输出怎么办?

也很简单,刷新缓存即可。如下所示:

<?php
echo "this is the first line <br />";
ob_flush();
flush();
sleep(2);
echo "this is the second line <br />";
ob_flush();
flush();
sleep(3);
echo "this is the third line <br />";
?>

实际上就是在每一个 echo 后面加上ob_flush(); flush(); 两个函数。

标签: php

相关文章推荐

添加新评论 (无需注册,可直接评论)