当前位置:首页 > PHP教程 > php高级应用 > 列表

php缓冲输出实例分析

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-05 20:33:12 浏览: 评论:0 

这篇文章主要介绍了php缓冲输出用法,以实例形式较为完整的分析了缓冲输出的具体实现过程,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php缓冲输出用法。分享给大家供大家参考。具体分析如下:

ob_start([string output_callback])- 打开输出缓冲区

所有的输出信息不在直接发送到浏览器,而是保存在输出缓冲区里面,可选得回调函数用于处理输出结果信息.

ob_end_flush - 结束(发送)输出缓冲区的内容,关闭输出缓冲区

实例代码如下:

  1. ob_start();          //打开缓冲区 
  2. echo "hello world";        //输出内容 
  3. $out=ob_get_clean();       //获得缓冲区内容并且结束缓冲区 
  4. $out=strtolower($out);       //将字符转换为小写 
  5. var_dump($out);        //输出结果 
  6. // 
  7.  
  8. if(!function_exists('ob_clean'))      //判断函数是否被定义 
  9. { 
  10.   function ob_clean()       //定义函数 
  11.   { 
  12.     if(@ob_end_clean()) 
  13.     { 
  14.       return ob_start(); 
  15.     } 
  16.     trigger_error("ob_clean() failed to delete buffer.no buffer to delete.",e_user_notice); 
  17.     return false; 
  18.   } 
  19. } 
  20. // 
  21.  
  22. header('content-type: multipart/x-mixed-replace;boundary=endofsection');  //发送标头 
  23. print "n--endofsectionn";           //输出内容 
  24. $pmt=array("-","","|","/");           //定义数组 
  25. for($i=0;$i<10;$i++)            //通过循环进行操作 
  26. { 
  27.   sleep(1);             //暂停执行 
  28.   print "content-type: text/plainnn";         //输出内容 
  29.   print "part $it".$pmt[$i % 4];          //输出内容 
  30.   print "--endofsectionn";           //输出内容 
  31.   ob_flush();             //发送缓冲区数据 
  32.   flush();              //刷新输出缓冲 
  33. } 
  34. print "content-type: text/plainnn";         //输出内容 
  35. print "the endn";            //输出内容 
  36. print "--endofsection--n";           //输出内容

Tags: php缓冲输出

分享到:

相关文章