dongzhong2674 2013-06-19 13:58
浏览 77
已采纳

输出缓冲? 为什么不?

I found few articles on the Internet that are suggesting use of ob_ functions, all of these emphase benefits, and there are no downsides of using functions mentioned.

My question is what are the downsides of using ob_ functions, or setting ini_set('output_buffering', '1'); ?

  • 写回答

4条回答 默认 最新

  • doudi8231 2013-06-19 14:14
    关注

    The cons of using output buffering entirely depend on the context of your usage.

    One of the biggest cons of output buffering is your runtime error messages or warnings may get suppressed, and you may sometimes end up with erroneous data.

    Consider this example:

    <?php
    function render_template() {
        ob_start();
        // Do some processing
        fetch_template_and_render();
        do_render();
        // end capture
        $output = ob_get_clean();
        return $output;
    }
    
    memchace::set( $some_key, render_template() );
    ?>
    

    If either of fetch_template_and_render or do_render throw run time errors, they will get dumped into your output, and eventually in this example will end up in the database or cache.

    Here are 2 snippets that demonstrate what I mean which you can try for yourself

    #1

    <?php 
        echo 1/0; 
    ?>
    

    outputs

    Warning: Division by zero on line 1
    

    #2

    <?php 
        ob_start();
        echo 1/0; 
        $var = ob_get_clean();
    ?>
    

    outputs nothing.

    To avoid such cases, you will need to be diligent about error checking and take precautions.

    When used diligently, ob_* functions are very powerful and super useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?