Question

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?

Was it helpful?

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.

OTHER TIPS

I think you're looking for the ob_get_clean() function.

See the difference between ob_get_clean(), ob_end_flush() and ob_end_clean().

ob_get_clean gets the current buffer contents and discards the current output buffer.

ob_end_flush discards the contents, but outputs the buffer first.

ob_end_clean discards the contents of the topmost output buffer. It will return nothing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top