PHP ob_get_level() Function

What is PHP ob_get_level() function?

ob_get_level() function lets you know in which output buffering level you’re at this moment.

Each output buffer represents one level of output buffering. By default, output buffering is on which you can find in the php.ini file. Each PHP script starts in the level 1 output buffer. If the output buffer is off in the php.ini file, you’re in level 0 in your script until you create one. A new output buffer level created with each ob_start() function.

Syntax:

ob_get_level()

Parameters:

This function has no parameter.

Return Values:

It returns an integer which represents buffer level. If outputs buffer is set off at php.ini file, it returns 0.

Examples:

Example 1:

<?php
   echo "Current Output Buffer level is: ".ob_get_level();
?>

Output:

Current Output Buffer level is: 1

Explanation:

Assumed that output buffer is on in the php.ini file. Else, the result would be 0.

Example 2:

<?php
 ob_start();
  echo "Current Output Buffer level is: " . ob_get_level() . "<br />";
   ob_start();
   echo "Current Output Buffer level is: " . ob_get_level();
?>

Output:

Current Output Buffer level is: 2
Current Output Buffer level is: 3

Explanation:

Assumed that output buffer is on in the php.ini file and the first buffer level starts here. The second buffer is added by ob_start() function in line #2. So, the buffer level is 2. Similarly, the third buffer is added by ob_start() function in line #4. So, the buffer level is 3.

PHP Version Support:

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

Summary: PHP ob_get_level() Function

PHP ob_get_level() function is a built-in PHP function and part of the PHP’s Output Control functions. It let you know the output buffer level from where the function is called.

Reference:

https://www.php.net/manual/en/function.ob-get-level.php