Open In App

PHP ob_get_length() Function

Last Updated : 04 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The ob_get_length() function is an inbuilt function in PHP that is used to get the length of the current output buffer. The output buffer length is the number of bytes in the buffer.

Syntax:

ob_get_length(): int|false

Parameters: This function does not accept any parameter.

Return Values: The ob_get_length() function returns the length of the current output buffer in bytes as an integer. If the output buffering is not active or if there is no content in the buffer then it will return “false”.

Program 1: The following program demonstrates the ob_get_length() function.

PHP




<?php
  
// Start output buffering
ob_start();
  
echo "This is content inside the buffer.";
  
$bufferLength = ob_get_length();
echo "Buffer length: " . $bufferLength . " bytes.";
  
ob_end_flush();
  
?>


Output:

This is content inside the buffer. Buffer length: 34 bytes

Program 2: The following program demonstrates the ob_get_length() function.

PHP




<?php
  
// Start output buffering
ob_start();
  
for ($i = 1; $i <= 10; $i++) {
    echo "Line " . $i . "<br>";
}
  
// Get the length of the buffer
$bufferLength = ob_get_length();
  
// Display the buffer length
echo "Buffer length: " . $bufferLength . " bytes.";
  
// Flush the buffer and
// send the contents to the client
ob_end_flush();
  
?>


Output:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Buffer length: 101 bytes.

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads