PHP Function Reference

PHP ob_get_length() Function



The PHP ob_get_length() function returns the length of the contents in the output buffer, in bytes. If the output buffer is not active, it returns false.

Syntax

ob_get_length()

Parameters

No parameter is required.

Return Value

Returns the length of the output buffer contents, in bytes, or false if no buffering is active.

Example:

The example below shows the usage of ob_get_length() function.

<?php
//turn on the output buffering
ob_start();

echo "Hello ";

//saving the content of output buffer and
//its length into variables
$output1 = ob_get_contents();
$len1 = ob_get_length();

echo "World";

//again, saving the content of output 
//buffer and its length into variables
$output2 = ob_get_contents();
$len2 = ob_get_length();

//clean the output buffer and turn off
//the output buffering
ob_end_clean();

var_dump($output1, $output2);
echo "length1: $len1 \nlength2: $len2";
?>

The output of the above code will be:

string(6) "Hello "
string(11) "Hello World"
length1: 6 
length2: 11

❮ PHP Output Control Reference

5