PHP Function Reference

PHP ob_get_contents() Function



The PHP ob_get_contents() function returns the contents of the output buffer without clearing it. If the output buffer is not active, it returns false.

Syntax

ob_get_contents()

Parameters

No parameter is required.

Return Value

Returns the contents of the output buffer or false, if output buffering isn't active.

Example: ob_get_contents() example

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

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

echo "Hello ";

//saving the content of output buffer 
//into $output1 variable
$output1 = ob_get_contents();

echo "World";

//again, saving the content of output 
//buffer into $output2 variable
$output2 = ob_get_contents();

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

var_dump($output1, $output2);
?>

The output of the above code will be:

string(6) "Hello "
string(11) "Hello World"

❮ PHP Output Control Reference