PHP ob_get_length() Function

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

Definition

In PHP, ob_get_length() returns the length in bytes of the contents currently stored in the active output buffer.

If no buffering is active, it returns false. It is useful for logging size, deciding whether anything was captured, or pairing with ob_get_contents() while you still keep the buffer.

Syntax

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

PHP
$bytes = ob_get_length();
CallWhat it does
ob_get_length()Returns the byte length of the active buffer, or false if none.
ob_start(); echo "hi"; ob_get_length()Returns 2 for the two characters.
ob_get_length() === falseTrue when output buffering is not active.

The length counts bytes in the buffer, not characters of a multibyte encoding in a special way beyond how PHP stored the string. Cleaning or flushing the buffer changes the length afterward.

Library declarations
PHP
ob_get_length(): int|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
int|falseThe length of the active buffer contents in bytes, or false when buffering is not active.

PHP Examples

Example 1 — Measure Growing Output

Echo in two steps and read the length after each step, then end-clean.

PHP
<?php
ob_start();
echo "Hello ";
$len1 = ob_get_length();
echo "World";
$len2 = ob_get_length();
ob_end_clean();
echo $len1, ", ", $len2, PHP_EOL;

How It Works

  1. "Hello " is six characters, so the first length is 6.
  2. "World" is appended inside the same buffer.
  3. The second length is 11.
  4. ob_end_clean() discards the buffer; only the printed lengths remain.

Example 2 — Zero Versus False

Compare the length of an empty active buffer with the result when buffering is off.

PHP
<?php
ob_start();
$empty = ob_get_length();
ob_end_clean();
$off = ob_get_length();
echo "empty=", var_export($empty, true), " off=", var_export($off, true), PHP_EOL;

How It Works

  1. ob_start() creates an empty buffer.
  2. ob_get_length() returns integer 0.
  3. ob_end_clean() turns buffering off.
  4. ob_get_length() now returns false, which var_export prints as false.

Example 3 — Length After Clean

Write text, clean the buffer, and show that the length goes back to zero while the level stays up.

PHP
<?php
ob_start();
echo "abcde";
$before = ob_get_length();
ob_clean();
$after = ob_get_length();
$level = ob_get_level();
ob_end_clean();
echo "before=", $before, " after=", $after, " level=", $level, PHP_EOL;

How It Works

  1. Five characters make the length 5.
  2. ob_clean() erases the contents.
  3. ob_get_length() returns 0 for the empty but still-active buffer.
  4. ob_get_level() is still 1 until ob_end_clean() runs.

Frequently Asked Questions

It returns how many bytes are currently stored in the active output buffer, or false if buffering is not active.
0 means a buffer is active but empty. false means there is no active output buffer.
No. It only reports the size. Use ob_clean(), ob_flush(), or an end_* function to change the buffer.
strlen(ob_get_contents()) matches ob_get_length() when a buffer is active. ob_get_length() avoids copying the whole string when you only need the size.
You can call it, but it returns false until something has started an output buffer.

Next: ob_get_level()

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

ob_get_level() 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