What is output buffering?

后端 未结 7 2320
不思量自难忘°
不思量自难忘° 2020-11-22 01:49

What is output buffering and why is one using it in PHP?

7条回答
  •  情书的邮戳
    2020-11-22 02:32

    ob_start();  // turns on output buffering
    $foo->bar();  // all output goes only to buffer
    ob_clean();  // delete the contents of the buffer, but remains buffering active
    $foo->render(); // output goes to buffer
    ob_flush(); // send buffer output
    $none = ob_get_contents();  // buffer content is now an empty string
    ob_end_clean();  // turn off output buffering
    

    Buffers can be nested, so while one buffer is active, another ob_start() activates a new buffer. So ob_end_flush() and ob_flush() are not really sending the buffer to the output, but to the parent buffer. And only when there is no parent buffer, contents is sent to browser or terminal.

    Nicely explained here: https://phpfashion.com/everything-about-output-buffering-in-php

提交回复
热议问题