How to capture the output with the php output buffer

Card Puncher Data Processing

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

See the dedicated page: How to solve Test code or tested code did not (only) close its own output buffers





Discover More
Card Puncher Data Processing
Datacadamia - Data all the things

Computer science from a data perspective
Card Puncher Data Processing
How to resolve: Test code or tested code did not (only) close its own output buffers ?

When running phpunit test case, you may get this warning message The solution is that generally that you needs to end up your test with a output buffer level of 1. Write at the end of your risky test,...
Card Puncher Data Processing
Php - Output (Echo/Print)

php knowns two functions that sends characters to the output device which is: the standard stream for a console / cli application the body of an http request for a web server echo print The...



Share this page:
Follow us:
Task Runner