PHP output buffering? What's the best practise?

后端 未结 3 966
北荒
北荒 2021-01-19 18:17

Further to my previous question, what\'s the best approach when I want to buffer PHP output until I have performed all processing? I want to buffer to leave myself the optio

3条回答
  •  梦毁少年i
    2021-01-19 18:52

    I open a buffer with ob_start(); ( http://php.net/manual/en/function.ob-start.php )

    Then anything that would normally be sent to the browser (except headers) is stored in the buffer until I close it. When I want to output or manipulate the buffer, I access it like this:

    $buffer = ob_get_clean(); ( http://php.net/manual/en/function.ob-get-clean.php )

    There are lots of other buffer options here:

    http://www.php.net/manual/en/ref.outcontrol.php

    This is the best way in my opinion because you don't have to keep adding items to the buffer; PHP is automatically capturing everything as long as the buffer is open.

提交回复
热议问题