Thursday, December 11, 2025
HomeLanguagesPHP ob_get_length() Function

PHP ob_get_length() Function

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32440 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6811 POSTS0 COMMENTS
Nicole Veronica
11950 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12023 POSTS0 COMMENTS
Shaida Kate Naidoo
6943 POSTS0 COMMENTS
Ted Musemwa
7195 POSTS0 COMMENTS
Thapelo Manthata
6889 POSTS0 COMMENTS
Umr Jansen
6880 POSTS0 COMMENTS