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_content();
  • 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

phpunit reports this warning message

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

when the level at the start of an unit test is not the same at the end.

Because it means that there was an error somewhere (functional or technical) that didn't capture the data in the output buffer generated.


Powered by ComboStrap