ob_get_status

Returns status information about the top output buffer, or about every active buffer level.

Signature
ob_get_status(bool $full_status = false): array
  • Added in PHP 4.2.0

Parameters

ParameterTypeDescription
$full_statusoptionalbool= falsetrue to return status for every active output buffer level, keyed by level. When false or omitted only the top level buffer is described.

Return value

arrayReturns an array of status information for the top output buffer, or one entry per active level when full_status is true; an empty array if output buffering is not active.

ob_get_status() describes the output buffer stack rather than its contents. Each entry contains:

KeyMeaning
nameName of the active output handler, e.g. default output handler
type0 for an internal handler, 1 for a user supplied one
flagsBitmask of the flags passed to ob_start()
levelNesting level, counting from zero
chunk_sizeChunk size in bytes
buffer_sizeBytes currently allocated for the buffer
buffer_usedBytes of data actually held

When to reach for it

Use it to debug output buffering you did not set up: to find out which handler is wrapping your page, whether a buffer can be cleaned or removed before you try, and how many levels a framework left open. Test flags against PHP_OUTPUT_HANDLER_CLEANABLE, PHP_OUTPUT_HANDLER_FLUSHABLE or PHP_OUTPUT_HANDLER_REMOVABLE to know in advance whether ob_clean(), ob_flush() or ob_end_clean() will succeed.

What trips people up

With no active buffer it returns an empty array, not false, so indexing it straight away produces an undefined-key warning. And its level is zero-based, one less than ob_get_level() reports for the same buffer.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Find out which output handler a framework or php.ini setting installed around your page.
  • Test the flags bitmask before calling ob_clean() or ob_end_clean() so the call cannot fail with a notice.
  • Log the full buffer stack when debugging duplicated or missing output.

Frequently asked questions

What does ob_get_status return when buffering is off?

An empty array. It never returns false, so use empty($status) rather than a false comparison before reading keys.

How do I get the status of every open output buffer?

Pass true: ob_get_status(true) returns one entry per level, keyed by the zero-based level number, innermost last.

Why is the level in ob_get_status one less than ob_get_level?

They use different bases: ob_get_status() numbers the first buffer 0, while ob_get_level() counts it as 1.

More examples

Identifying a user supplied handler

The output is wrapped in square brackets by the handler, and reads name: tagWrapper, type: 1. type 1 marks a userland callback, as opposed to type 0 for PHP's built-in default handler.

Example · php
<?php
function tagWrapper(string $buffer, int $phase): string {
    return "[$buffer]";
}

ob_start('tagWrapper');
$s = ob_get_status();
echo "name: {$s['name']}, type: {$s['type']}\n";
ob_end_flush();

Checking flags before cleaning

chunk_size echoes back the 4096 passed to ob_start(), and the flag test reports yes because the default flag set includes PHP_OUTPUT_HANDLER_CLEANABLE. Testing the bitmask first avoids the E_NOTICE a failing ob_clean() would raise.

Example · php
<?php
ob_start(null, 4096);
$s = ob_get_status();
$cleanable = (bool) ($s['flags'] & PHP_OUTPUT_HANDLER_CLEANABLE);
ob_end_clean();

echo "chunk_size: {$s['chunk_size']}\n";
echo "safe to call ob_clean(): ", $cleanable ? 'yes' : 'no', "\n";

Discussion

  • Be the first to comment on this lesson.