W3docs

PHP ob_get_level() Function: Everything You Need to Know

As a PHP developer, you may need to get the current level of output buffering. The ob_get_level() function is a built-in function in PHP that allows you to get

ob_get_level() returns how many output buffers are currently active — that is, how deeply output buffering is nested. Output buffers in PHP form a stack: each call to ob_start() pushes a new buffer onto the stack, and each ob_end_* call pops one off. ob_get_level() tells you how tall that stack is right now.

This is the safest way to ask "is output buffering on?" and "how many layers are there?" without touching the buffered content. This page covers the function's signature, return value, how nesting works, and the common reasons you'd reach for it.

Syntax

ob_get_level(): int

The function takes no arguments and returns an int:

  • 0 — no output buffering is active.
  • 1 — exactly one buffer is open.
  • 2, 3, … — that many buffers are nested on top of each other.

It never throws and never produces output of its own, so it is safe to call anywhere.

A basic example

When no buffer has been started, the level is 0. After one ob_start() it becomes 1:

<?php

echo ob_get_level();   // 0 — nothing buffered yet

ob_start();
echo ob_get_level();   // 1 — one buffer is now active

ob_end_clean();
echo ob_get_level();   // 0 — buffer popped, back to baseline

Note that the two echo calls inside the buffer are themselves captured by the buffer; ob_end_clean() discards that captured text, so the only thing that reaches the browser is the final 0. To see the intermediate values during development, store them in variables first or use ob_get_clean() to release the buffer.

Counting nested buffers

Because buffers stack, calling ob_start() twice gives you a level of 2. This is the behaviour that makes ob_get_level() genuinely useful:

<?php

ob_start();                 // level 1
ob_start();                 // level 2

$level = ob_get_level();    // 2

ob_end_clean();             // level 1
ob_end_clean();             // level 0

echo "Deepest nesting was: {$level}";  // Deepest nesting was: 2

Frameworks, templating engines, and shutdown handlers frequently open their own buffers, so on a real request the level may already be 1 or more before your own code runs.

When would I use it?

  • Defensive cleanup. Before sending headers or starting a fresh buffer, drain whatever is open so stray output doesn't break your response:

    <?php
    // Discard any buffers a framework or earlier code left open
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
  • Conditional flushing. Only flush if a buffer is actually active, avoiding an ob_end_flush() warning when none exists:

    <?php
    if (ob_get_level() > 0) {
        ob_end_flush();
    }
  • Debugging buffer leaks. Logging ob_get_level() at the start and end of a request helps you spot a buffer that was opened but never closed.

Gotchas

  • ob_get_level() does not read or clear the buffer — for that use ob_get_contents() or ob_get_clean().
  • Calling ob_end_clean() / ob_end_flush() when the level is 0 raises a notice. Guard such calls with an ob_get_level() > 0 check, as shown above.
  • A high zlib.output_compression or framework setup can mean the level is non-zero from the very first line of your script — never assume it starts at 0.

Conclusion

ob_get_level() reports the current depth of PHP's output-buffer stack: 0 when buffering is off, and a higher number for each nested ob_start(). Because it inspects the stack without consuming the buffered content, it's the right tool for guarding buffer operations and for cleanly draining every open buffer before you send your final response.

Practice

Practice
What does the ob_get_level() PHP function do?
What does the ob_get_level() PHP function do?
Was this page helpful?