ob_get_length
Returns the number of bytes currently held in the active output buffer.
ob_get_length(): int|false- Added in PHP 4.0.2
Return value
int|falseReturns the length of the output buffer contents in bytes, or false if no buffering is active.
ob_get_length() reports how many bytes the active output buffer is currently holding, without copying the data out. It is the cheap alternative to strlen(ob_get_contents()): no string is materialised, so it stays fast even on a multi-megabyte buffer.
When to reach for it
The main use is flow control while streaming. Buffer output, and whenever the length crosses a threshold call ob_flush() and flush() — that keeps peak memory bounded no matter how large the total response is. It is also the quickest way to ask "did this template produce anything?" before deciding whether to render a surrounding block, and a convenient way to measure a response body for a Content-Length header or a log field.
What trips people up
Two things. First, it counts bytes, not characters, so a UTF-8 page reports more than its character count — that is correct for Content-Length but wrong for a "characters written" metric. Second, the return type is int|false: 0 (buffer active but empty) and false (no buffer at all) are both falsy, so if (!ob_get_length()) conflates two very different states. Use === false.
It measures only the innermost buffer; text already flushed into a parent no longer counts.
Example
When to use it
- Flush a streaming response whenever the buffer passes a size threshold to keep memory flat.
- Check whether a buffered template produced any output before rendering a wrapper around it.
- Measure a response body for a Content-Length header or an access-log field.
Frequently asked questions
What is the difference between ob_get_length and strlen(ob_get_contents())?
They give the same number, but ob_get_length() does not copy the buffer into a string, so it is far cheaper on large buffers.
Does ob_get_length count characters or bytes?
Bytes. A multi-byte UTF-8 string reports more than its character count, which is what you want for Content-Length but not for a character metric.
Why does ob_get_length return false?
Because no output buffer is active. An active buffer with nothing in it returns 0, so distinguish the two with a === false comparison.
More examples
Telling an empty buffer from a full one
The dumps read int(0) and int(11). Both are integers, not false, which is the distinction that matters: an active but empty buffer is 0, while false would mean there was no buffer at all.
<?php
ob_start();
$empty = ob_get_length(); // nothing written yet
echo "some output";
$filled = ob_get_length();
ob_end_clean();
echo "empty buffer: ";
var_dump($empty);
echo "after writing: ";
var_dump($filled);Flushing on a size threshold
All four rows print, and the counter reads 2: each row is 6 bytes, so the buffer reached 12 after rows 2 and 4 and was released both times, never growing beyond two rows.
<?php
ob_start();
$flushes = 0;
foreach (range(1, 4) as $i) {
echo "row $i\n";
if (ob_get_length() >= 12) {
ob_flush();
flush();
$flushes++;
}
}
ob_end_flush();
echo "flushed $flushes time(s) mid-loop\n";
Discussion