W3docs

PHP ob_get_length() Function: Everything You Need to Know

As a PHP developer, you may need to get the length of the output buffer. The ob_get_length() function is a built-in function in PHP that allows you to get the

ob_get_length() returns the number of bytes currently sitting in PHP's active output buffer — the content that has been captured by ob_start() but not yet sent to the browser. It is handy when you want to measure generated output before deciding what to do with it: enforce a size limit, build a Content-Length header, log a payload size, or simply check whether anything has been written yet.

This page covers the function's signature, its return value, the gotchas around nested buffers, and a few practical patterns.

Syntax

ob_get_length(): int|false

The function takes no arguments. It returns:

  • an integer — the length in bytes of the data in the active output buffer, or
  • false — if output buffering is not active (no buffer has been started, or all of them have already been closed).

Because it can return false, always compare with === rather than a loose check if you need to distinguish "no buffer" from a buffer of length 0.

A basic example

<?php

ob_start();
echo "This will be buffered";   // 21 bytes
$length = ob_get_length();
ob_end_clean();                 // discard the buffer

echo "Buffered length was: $length"; // Buffered length was: 21

Here ob_start() begins buffering, so echo writes into the buffer instead of to the browser. ob_get_length() reports 21 (the byte length of the string). ob_end_clean() then throws the buffered content away — only the final echo reaches the output.

Note that the length is measured in bytes, not characters. A multibyte UTF-8 string will report more bytes than it has visible characters.

Detecting whether a buffer is active

When no buffering is in effect, the function returns false:

<?php

var_dump(ob_get_length()); // bool(false)

ob_start();
echo "hi";
var_dump(ob_get_length()); // int(2)
ob_end_clean();

This makes ob_get_length() a quick way to test "is anything currently being buffered?" — closely related to ob_get_level(), which tells you how many nested buffers are open.

Nested buffers: only the active one is measured

PHP buffers can be stacked. ob_get_length() always reports the length of the topmost (innermost, currently active) buffer — never the combined total:

<?php

ob_start();
echo "outer";              // 5 bytes in the outer buffer

ob_start();
echo "inner text";         // 10 bytes in the inner buffer
$inner = ob_get_length();  // measures the active (inner) buffer
ob_end_clean();            // discard inner

$outer = ob_get_length();  // outer is active again
ob_end_clean();            // discard outer

echo "inner=$inner outer=$outer"; // inner=10 outer=5

If you need the full picture, inspect each level with ob_get_level() and peel the buffers off one at a time.

A practical pattern: skip empty output

A common use is to avoid emitting anything (and the overhead of related work) when a buffered fragment turned out empty:

<?php

ob_start();
// ... template / partial that may or may not produce output ...

if (ob_get_length() > 0) {
    // there is real content — send it
    ob_end_flush();
} else {
    // nothing was generated — drop the empty buffer
    ob_end_clean();
}

To read the buffered bytes themselves instead of just their length, reach for ob_get_contents(); to grab the contents and close the buffer in one call, use ob_get_clean().

Conclusion

ob_get_length() reports the byte length of PHP's active output buffer, or false when no buffer is open. Remember three things: it counts bytes, it measures only the active buffer in a nested stack, and a false return means buffering is off. Combined with the rest of the output-control functionsob_get_contents(), ob_get_clean(), and ob_get_level() — it lets you measure and manage generated output precisely before it ever reaches the browser.

Practice

Practice
What is the function of ob_get_length() in PHP?
What is the function of ob_get_length() in PHP?
Was this page helpful?