PHP 8.4.0 RC4 available for testing

ob_get_level

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

ob_get_level返回输出缓冲机制的嵌套级别

说明

ob_get_level(): int

返回输出缓冲机制的嵌套级别。

参数

此函数没有参数。

返回值

返回嵌套的输出缓冲处理程序的级别;或者是,如果输出缓冲区不起作用,返回零。

警告

ob_get_level()ob_get_status() 之间相同级别的值相差 1。对于 ob_get_level(),第一级为 1。而对于 ob_get_status(),第一级为 0

参见

添加备注

用户贡献的备注 3 notes

up
55
Anonymous
12 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:
<?php
if (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:
<?php
if (!ob_get_level()) ob_start();
?>
up
10
Anonymous
9 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

<?php

function 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();
}
up
2
bonzini at gnu dot org
20 years ago
Even under older PHP, you can decide if output buffering is active (i.e. ob_get_level() > 0) using

<?php $ob_active = ob_get_length () !== FALSE ?>

Paolo
To Top