PHP - Output Buffer

About

An output buffer is a mechanism to capture all input of output function

Note that once the buffer is above its max limit set by output-buffering (get its actual size with ob_get_length), there is automatic flush.

Basic

  • You create a capture session of the buffer with ob_start
ob_start();
  • The php script generates output
echo "My output"
print "Whatever"
ob_get_contents();
  • You clean and end the actual capture session with ob_end_clean
ob_end_clean();

Nested / Level

This is a nested feature meaning that a sub-function may also create and end a capture session (ob_start, ob_end) to capture the output of its own instructions

ie:

  • A root function can start to capture the output with ob_start (making the level of ob_get_level to 1)
  • A sub-function of function can also start to capture the output with ob_start (making the level of ob_get_level to 2)
  • The sub-function can get and the capture with ob_end (making the level of ob_get_level back to 1)
  • The root function can get and the capture with ob_end (making the level of ob_get_level back to 0)

Support

phpunit: Test code or tested code did not (only) close its own output buffers


Powered by ComboStrap