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