ab
V2EX  ›  PHP

PHP 菜鸟请教一下 ob_start()的用法

  •  
  •   ab · Jan 3, 2020 · 4183 views
    This topic created in 2349 days ago, the information mentioned may be changed or developed.
    <?php
    ob_start('callback');

    echo 'test';

    function callback($value){
    @file_put_contents($_SERVER["DOCUMENT_ROOT"].'/cache.html', $value); //cache file
    ob_end_flush();
    //return $value;
    }
    ?>


    回调中,为什么用 ob_end_flush 不输出内容,而 return $value 却可以呢?
    1 replies    2020-01-03 21:07:53 +08:00
    ab
        1
    ab  
    OP
       Jan 3, 2020
    自己回答,文档没看清楚。

    ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() 不能从一个回调函数中调用。 如果从回调函数中调用了它们,产生的行为是不明确的。 如果想要删除缓冲区的内容,从回调函数中返回一个"" (空字符串)。 更不能从一个回调函数中使用像 print_r($expression, true) 或 highlight_file($filename, true) 一样的输出缓冲函数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3297 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:10 · PVG 21:10 · LAX 06:10 · JFK 09:10
    ♥ Do have faith in what you're doing.