PHP Function Reference

PHP ob_get_level() Function



The PHP ob_get_level() function returns the nesting level of the output buffering mechanism. If the output buffer is not active, it returns zero.

Syntax

ob_get_level()

Parameters

No parameter is required.

Return Value

Returns the level of nested output buffering handlers or zero if output buffering is not active.

Example:

The example below shows the usage of ob_get_level() function.

<?php
echo "Buffer level: ".ob_get_level()."\n";

//adding first output buffer
ob_start();
echo "Buffer level: ".ob_get_level()."\n";

//adding second output buffer
ob_start();
echo "Buffer level: ".ob_get_level()."\n";

//adding third output buffer
ob_start();
echo "Buffer level: ".ob_get_level()."\n";

//flushing and closing all output buffers
while(ob_get_level() != 0) {
  ob_end_flush();
}
?>

The output of the above code will be:

Buffer level: 0
Buffer level: 1
Buffer level: 2
Buffer level: 3

❮ PHP Output Control Reference