Wednesday, July 3, 2024
HomeLanguagesPhpPHP 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!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments