略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ob_get_level

2024-12-22

ob_get_level

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

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

说明

ob_get_level(): int

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

返回值

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

参见

add a noteadd a note

User Contributed Notes 5 notes

up
47
Anonymous
10 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
6
Anonymous
7 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
17 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
up
-14
mark dot richards at massmicro dot com
10 years ago
It was noted that ob_get_level() reports 1 prior to starting buffering, and in php 5.3.6-13 I can confirm this:

    <?php
     
echo ob_get_level(); // --> 1
   
?>

It seems an internal counter for the output buffering needs to be initialized.  To start things off properly, this appears to do:

    <?php
      ob_end_clean
(); 
      echo
ob_get_level(); // --> 0
   
?>
up
-10
Anonymous
17 years ago
Sometimes, ob_get_level() may be off by 1 because at the start of the script, it will return 1 even if ob_start() has never been called (and clearing the output buffer via ob_end_clean() and the like can be done without error).  As a result, the first ob_start() will have an ob_get_level() of 2, the second will be 3, and so on.

I'm not sure if this is a PHP 5 thing or [if it's related to when a] server is set to gzip all html documents.

Also, up until at least PHP 5.0.4 (current version), ob_get_level() will always return 0 inside a destructor.  This happens because the garbage collection for output buffers has already done before the destructor is called.  If you want to do something with the output buffer from within an object before the script exits, using a callback function with ob_start() is the way to go.

官方地址:https://www.php.net/manual/en/function.ob-get-level.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3