ob_get_level

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

ob_get_levelRetorna o nível de aninhamento do mecanismo de buffer de saída

Descrição

ob_get_level(): int

Retorna o nível de aninhamento do mecanismo de buffer de saída.

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

Retorna o nível de manipuladores de buffer de saída aninhados ou zero se o buffer de saída não estiver ativo.

Cuidado

Os valores para níveis idênticos entre ob_get_level() e ob_get_status() são deslocados em uma unidade. Para ob_get_level() o primeiro nível é 1, enquanto que para ob_get_status() o primeiro nível é 0.

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
55
Anonymous
13 years ago
For users confused about getting "1" as a return value from ob_get_level at the beginning of a script: this likely means the PHP ini directive "output_buffering" is not set to off / 0. PHP automatically starts 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:<?phpif (ob_get_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:<?phpif (!ob_get_level()) ob_start();?>
up
9
Anonymous
10 years ago
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<?phpfunction getView($view){    $level = ob_get_level();     ob_start();     try    {        include $view;    }    catch (Exception $e)    {        while (ob_get_level() > $level)        {            ob_end_clean();        }        throw $e;    }    return ob_get_clean();}
To Top