php 缓冲输出,PHP关闭输出缓冲的方法

PHP的缓冲功能确实能提高页面的效率,但是有时候我们还是希望PHP关闭缓冲,立即输出我们的代码,尤其是在运行费时的任务程序时.

PHP5默认打开了输出缓冲.在输出内容之前需要先执行 “ob_flush()”,清空缓冲,再执行”flush()”强制输出内容. 当然,我们也可以在最前面就把缓冲关闭,执行”ob_end_clean()”;

从代码看出,PHP有两种”缓冲”机制在运行,必须在清空缓冲后再强制输出,浏览器才能立即看到输出的内容.

以下是两种方法:

//方法1

for($i=0; $i<10; $i++){

echo “$i
n”;

ob_flush();

flush();

sleep(2);

}

//方法2

ob_end_flush();

for($i=0; $i<10; $i++){

echo “$i
n”;

flush();

sleep(2);

}

?>

ob_end_clean — Clean (erase) the output buffer and turn off output buffering

说明

bool ob_end_clean ( void )

This function discards the contents of the topmost output buffer and turns off this output buffering. If you want to further process the buffer’s contents you have to call? ob_get_contents()? before ob_end_clean() as the buffer contents are discarded when ob_end_clean() is called.

ob_flush

(PHP 4 >= 4.2.0, PHP 5)

ob_flush — Flush (send) the output buffer

说明

void ob_flush ( void )

This function will send the contents of the output buffer (if any). If you want to further process the buffer’s contents you have to call ob_get_contents() before ob_flush() as the buffer contents are discarded after ob_flush() is called.

This function does not destroy the output buffer like ob_end_flush() does.

flush

flush

(PHP 4, PHP 5)

flush — 刷新输出缓冲

说明

void flush ( void )

刷新PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。

flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响。因此,必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。

个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止。

有些Apache的模块,比如mod_gzip,可能自己进行输出缓存,这将导致flush()函数产生的结果不会立即被发送到客户端浏览器。

甚至浏览器也会在显示之前,缓存接收到的内容。例如 Netscape 浏览器会在接受到换行或 html 标记的开头之前缓存内容,并且在接受到 标记之前,不会显示出整个表格。

一些版本的 Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,所以必须发送一些额外的空格来让这些浏览器显示页面内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值