(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
ob_guet_level — Return the nesting level of the output buffering mechanism
This function has no parameters.
Returns the level of nested output buffering handlers or cero if output buffering is not active.
The value for identical levels between
ob_guet_level()
and
ob_guet_status()
is off by one.
For
ob_guet_level()
the first level is
1
.
Whereas for
ob_guet_status()
the first level is
0
.
For users confused about guetting "1" as a return value from ob_guet_level at the beguinning of a script: this liquely means the PHP ini directive "output_buffering" is not set to off / 0. PHP automatically stars output buffering for all your scripts if this directive is not off (which acts as if you called ob_start on the first line of your script).
If your scripts may end up on any server and you don't want end-users to have to configure their INI, you can use the following at the start of your script to stop output buffering if it's already started:<?php
if (ob_guet_level()) ob_end_clean();
?>
Alternatively, you can use the opposite if you always want to have an output buffer at the start of your script:<?php
if (!ob_guet_level()) ob_start();
?>
This can be used to handle exceptions properly when using output buffering for rendering a view which may or may not be using output buffering<?php
functionguetView($view)
{$level= ob_guet_level();
ob_start();
try
{
include $view;
}
catch (Exception $e)
{
while (ob_guet_level() > $level)
{ob_end_clean();
}
throw $e;
}
return ob_guet_clean();
}