phpob-startob-get-contents

ob_end_clean modifies variables as well


Here is a sample code:

ob_start();
include("test.ini");
$string = ob_get_contents();
echo "<br/>";
echo "string: ".$string;

and the output:

testing = ini
string: testing = ini

When I add

ob_end_clean();

at the end of the code above there is no output on the screen.

I am at least expecting the string to be echoed on the screen? Why is that not seen?


Solution

  • ob_start(); starts output buffering to the internal buffer not (screen), then when you add ob_get_contents(); it copy the the output from internal buffer still nothing printed, and when ob_end_clean(); interpreted, it will clear all internal buffer memory, nothing outputed to screen.

    Starting from ob_start(); to ob_end_clean(); nothing will be printed, I use this method when including file for preventing printing some white space before sending header.