The code snippet is:
ob_start();
echo 'ok';
ob_get_contents();
exit;
Expected result: should print nothing.
Actual result: for some reason ok has printed
The code snippet is:
ob_start();
echo 'ok';
ob_get_contents();
exit;
Expected result: should print nothing.
Actual result: for some reason ok has printed
2
Answers
The manual page about output buffers explains:
Since you only retrieved the content of the buffer, but did not "clean" it with ob_clean(), ob_end_clean() or ob_get_clean(), the content is still in the buffer when your page finishes, and is flushed to output.
The reason why your code prints "ok" instead of nothing is because the output buffer is flushed and turned off by PHP’s shutdown process when exit() is called. To prevent this, you can use ob_end_clean() to clean the buffer before calling exit().
The following code will explain the concept better.