Other OB functions

int ob_get_length ( void )

int ob_get_level ( void )

array ob_list_handlers ( void )

There are three other output buffering functions that are of interest: ob_get_length(), ob_get_level(), and ob_list_handlers(). Both take no parameters and return a number - for ob_get_length(), the return value is the number of bytes held in the buffer, and for ob_get_level() it is the "nest count". Ob_get_level() returns 0 if you are not within an output buffer, 1 if you have got one open, 2 if you have got two, etc.

Using ob_get_level() it is possible to recursively close and flush/clean all open buffers if you have an error, and ob_get_length() is helpful if you want to send a custom HTTP Content-Length header - although that is for advanced users only!

Finally, the ob_list_handlers() function takes no parameters and returns an array of any output handlers currently in effect. If output buffering is turned on, you should get back an array containing "default output handler", if you're using gzip to compress your buffer you should get "ob_gzhandler", and if you've used URL rewriting you should get "URL-Rewriter".


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Flushing output >>

Previous chapter: Reading buffers

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.