ob_list_handlers

Returns the names of the output handlers currently in use, from the outermost buffer inwards.

Signature
ob_list_handlers(): array
  • Added in PHP 4.3.0

Return value

arrayReturns an array of the output handlers in use, or an empty array when no output buffer is active. A buffer started without a callback (including one created by the output_buffering INI setting) is reported as "default output handler"; a callable is reported by its fully qualified name, an invokable object as its Class::__invoke, and a closure as "Closure::__invoke".

ob_list_handlers() reports which output handlers are stacked on the output buffers that are currently open. The array is ordered from the outermost buffer (index 0) to the innermost, which is the reverse of the order output actually passes through them.

When to reach for it

It is an inspection tool, so it earns its place in code that does not control the whole request. Before installing your own compression buffer, check that ob_gzhandler is not already there. Before writing binary data such as a PDF or CSV download, confirm no HTML-minifying or template handler is sitting in the stack ready to mangle it. In a framework or a test harness, use it to prove your middleware left the buffer stack exactly as it found it.

What the names mean

A plain ob_start() with no callback shows up as default output handler, and so does the buffer PHP opens for you when the output_buffering INI directive is on. Named functions appear under their own name, an object with __invoke() under Class::__invoke. An empty array means nothing is buffered at all — equivalent to ob_get_level() returning 0.

If you need more than names, ob_get_status(true) returns the same handlers plus each buffer's level, chunk size, flags and used bytes.

Example

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

When to use it

  • Check whether ob_gzhandler is already installed before adding your own compression buffer.
  • Verify no HTML post-processing handler is active before streaming a binary download such as a PDF or CSV.
  • Assert in a test that middleware closed every buffer it opened and left no handler behind.
  • Log the active handler stack when debugging output that arrives mangled, truncated or in the wrong order.

Frequently asked questions

How do I check if output buffering is already on in PHP?

An empty array from ob_list_handlers() means no buffer is active; a non-empty one lists the handlers in use. ob_get_level() answers the same question as an integer if you only need the nesting depth.

Why does ob_list_handlers return "default output handler"?

That is the name PHP gives a buffer started without a callback, including the one created automatically when the output_buffering INI directive is enabled. It means output is being buffered but not transformed.

What is the difference between ob_list_handlers and ob_get_status?

ob_list_handlers() returns just the handler names, while ob_get_status(true) returns a full record per buffer including level, chunk size, buffer size, used bytes and flags. Use ob_get_status() when you need to know how a buffer is configured, not only what is filtering it.

More examples

How closures and invokable objects are named

The output is Array ( [0] => Minifier::__invoke [1] => Closure::__invoke ). An invokable object is identified by its class and method, but every closure collapses to the same "Closure::__invoke" string, so closures cannot be told apart in this list.

Example · php
<?php
class Minifier
{
    public function __invoke(string $buffer, int $phase = 0): string
    {
        return preg_replace('/\s+/', ' ', $buffer);
    }
}

ob_start(new Minifier());
ob_start(fn(string $b, int $p = 0): string => $b);

$handlers = ob_list_handlers();
ob_end_clean();
ob_end_clean();

print_r($handlers);

Guard against a handler that would corrupt binary output

in_array() on the returned names is the practical way to test for one specific handler. The script prints "minifier removed", then var_dump shows string(13) "html_minifier" as the innermost entry that was found before the buffer was closed.

Example · php
<?php
function html_minifier(string $b, int $p = 0): string
{
    return trim($b);
}

ob_start('html_minifier');
$handlers = ob_list_handlers();

if (in_array('html_minifier', $handlers, true)) {
    ob_end_clean(); // drop it before sending binary data
    echo "minifier removed\n";
}

echo "innermost handler now: ";
var_dump(end($handlers));

Distinguish "no buffering" from "buffering with no callback"

The first call prints an empty Array ( ), meaning no buffer is open. After ob_start() with no callback the same call reports Array ( [0] => default output handler ) — buffering is on even though no handler function was supplied.

Example · php
<?php
print_r(ob_list_handlers()); // nothing active yet
echo "\n";

ob_start();
$active = ob_list_handlers();
ob_end_clean();

print_r($active);

Discussion

  • Be the first to comment on this lesson.