PHP ob_get_level() Function
Last Updated :
28 Sep, 2023
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();
echo "This is content inside the buffer." ;
ob_start();
$bufferingLevelNew = ob_get_level();
echo "This is content inside the new buffer." ;
ob_end_flush();
$bufferingLevelAfterEnd = ob_get_level();
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
ob_start();
function checkOutputBufferLevel()
{
$bufferingLevel = ob_get_level();
echo "Output buffering level: " . $bufferingLevel . "<br>" ;
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
Share your thoughts in the comments
Please Login to comment...