PHP ob_list_handlers() Function

What is PHP ob_list_handlers() function?

If you want to know all the handler’s names being used in your current output buffer, use ob_list_handlers() function.

Syntax:

ob_list_handlers()

Parameters:

This function has no parameter.

Return Values:

The function returns an array with handlers list. Each array element contains one handler name.

  • It returns “default output handler” If output buffering is enabled in php.ini file.

Examples:

<pre>
<?php
   print_r(ob_list_handlers());
   ob_end_flush();

   ob_start();
   print_r(ob_list_handlers());
   ob_end_flush();

   ob_start("ob_gzhandler");
   print_r(ob_list_handlers());
   ob_end_flush();

   ob_start(function($something) { return$something;});
   print_r(ob_list_handlers());
   ob_end_flush();
?>
</pre>

Output:

Array
(
[0] => default output handler
)
Array
(
[0] => default output handler
)
Array
(
[0] =>ob_gzhandler
)
Array
(
[0] =>Closure::__invoke
)

Explanation:

The first array element is “default output handler” because the output buffering is enabled in php.ini file. The second array element is “default output handler” because the output_buffering is enabled in php.ini file and no output_handler is set to ob_start() function. The third array element is “ob_gzhandler” because this is a output handler used in ob_start() function. The fourth array element is “Closure::__invoke” because the closure (anonymous function) is used as callable.

PHP Version Support:

PHP 4>= 4.3.0, PHP 5, PHP 7, PHP 8

Summary: PHP ob_list_handlers() Function

The ob_list_handlers() function is a built-in PHP function and part of the PHP’s Output Control functions. Call it when you want to know all the output handlers.

Reference:

https://www.php.net/manual/en/function.ob-list-handlers.php