PHP ob_get_level() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Output Control

Definition

In PHP, ob_get_level() returns how many nested output buffers are currently active.

With no buffering, the level is 0. Each successful ob_start() raises the level by one. Ending a buffer with ob_end_clean(), ob_end_flush(), ob_get_clean(), or ob_get_flush() lowers it by one. Use the level to understand nesting and to loop until all buffers are closed.

Syntax

Call ob_get_level() with the values you need. Start with this line.

PHP
$depth = ob_get_level();
CallWhat it does
ob_get_level()Returns 0 when no output buffer is active.
ob_start(); ob_get_level()Returns 1 after the first ob_start().
ob_start(); ob_start(); ob_get_level()Returns 2 with two nested buffers.

ob_get_level() never fails; it always returns an integer. The first active buffer is level 1 here, while ob_get_status() reports that same buffer with a zero-based "level" key of 0. Do not mix those two numbering styles.

Library declarations
PHP
ob_get_level(): int

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
intThe number of active nested output buffers. 0 means buffering is off.

PHP Examples

Example 1 — Watch the Level Rise and Fall

Start one buffer, read the level, then end-clean and read again.

PHP
<?php
$before = ob_get_level();
ob_start();
$during = ob_get_level();
ob_end_clean();
$after = ob_get_level();
echo $before, " -> ", $during, " -> ", $after, PHP_EOL;

How It Works

  1. With no buffering, ob_get_level() is 0.
  2. ob_start() creates one buffer, so the level becomes 1.
  3. ob_end_clean() removes that buffer.
  4. The level returns to 0.

Example 2 — Nested Buffers Raise the Level

Open two buffers and print the level after each start, then close both.

PHP
<?php
ob_start();
echo "L1=", ob_get_level(), PHP_EOL;
ob_start();
echo "L2=", ob_get_level(), PHP_EOL;
ob_end_clean();
ob_end_clean();
echo "done=", ob_get_level(), PHP_EOL;

How It Works

  1. The first ob_start() makes the level 1.
  2. The second ob_start() nests another buffer at level 2.
  3. Each ob_end_clean() removes only the top buffer.
  4. After two end-cleans, the level is 0 again.

Example 3 — Close Every Active Buffer

Start three buffers, then loop on ob_get_level() until none remain.

PHP
<?php
ob_start();
ob_start();
ob_start();
$opened = ob_get_level();
while (ob_get_level() > 0) {
    ob_end_clean();
}
echo "opened=", $opened, " now=", ob_get_level(), PHP_EOL;

How It Works

  1. Three nested ob_start() calls make the level 3.
  2. The while loop runs while the level is greater than 0.
  3. Each ob_end_clean() lowers the level by one.
  4. When the loop ends, no buffers remain.

Frequently Asked Questions

It returns the nesting depth of active output buffers. 0 means none are active; each ob_start() adds one level.
ob_start() is the main entry that turns buffering on and raises the level. Nested calls raise it further until you end those buffers.
Not the numbering. ob_get_level() counts from 1 for the first buffer. The "level" key inside ob_get_status() is zero-based, so the first buffer is 0 there.
No. It always returns an integer, including 0 when buffering is off.
Loop while (ob_get_level() > 0) and call ob_end_clean() or ob_end_flush() inside the loop, depending on whether you want to discard or send.

Next: ob_get_status()

ob_get_status() returns an array of status details about the active output buffer, or abou.

ob_get_status() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful