Friday, September 5, 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
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS