Thursday, September 4, 2025
HomeLanguagesPHP ob_get_level() Function

PHP ob_get_level() Function

The ob_get_level() function is an inbuilt function in PHP that is used to get the current output buffer level in a nested level. Output buffering is a feature in PHP that allows you to capture and manipulate output before it is sent to the browser or client.

Syntax

ob_get_level(): int

Parameter

This function does not accept any parameters.

Return Values

The ob_get_level() function returns an integer value that represents the current value of the output buffering.

Program 1: The following program demonstrates the ob_get_level() Function.

PHP




<?php
ob_start();
  
$bufferingLevel = ob_get_level();
  
// Output some content
echo "This is content inside the buffer.";
  
// Start a new output buffer
ob_start();
  
// Get the new output buffering level
$bufferingLevelNew = ob_get_level();
  
// Output more content inside the new buffer
echo "This is content inside the new buffer.";
  
// End the new buffer
ob_end_flush();
  
// Check the output buffering level
// after ending the new buffer
$bufferingLevelAfterEnd = ob_get_level();
  
// End the original buffer
ob_end_flush();
?>


Output:

This is content inside the buffer.This is content inside the new buffer. 

Program 2: The following program demonstrates the ob_get_level() Function.

PHP




<?php
// Start output buffering
ob_start();
  
// Function to check output buffering level
// and perform actions accordingly
function checkOutputBufferLevel()
{
    $bufferingLevel = ob_get_level();
  
    // Display the output buffering level
    echo "Output buffering level: " . $bufferingLevel . "<br>";
  
    // Perform actions based on the output buffering level
    if ($bufferingLevel === 1) {
        echo "You are in the top-level buffer.<br>";
    } elseif ($bufferingLevel > 1) {
        echo "You are in a nested buffer.<br>";
    } else {
        echo "Output buffering is not active.<br>";
    }
}
  
checkOutputBufferLevel();
  
echo "This is content inside the buffer.<br>";
  
ob_end_flush();
?>


Output:

Output buffering level: 1
You are in the top-level buffer.
This is content inside the buffer.

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

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS