Search code examples
phpoutput-buffering

Why does my PHP output buffering not work


Never used output buffering before but reading about it seems pretty straight forward. So this is what I did for testing:

<?php ob_start(); ?>

<p>Welcome to my homepage</p>

The problem is that the script is actually printing the paragraph onto my browser even though I have not flushed yet.

Is this not how output buffering should be used??


Solution

  • That's standard behavior. PHP will flush any open output buffers as part of script shutdown. That means you don't HAVE to call ob_flush() or the end/clean calls. It'll just happen automatically when execution of the script ends.

    The only times you really do want to force a flush is if you're building up a "large" page and don't want it sucking up the script's memory limit, or you've gone past the point in your code where you'd need to do any pre-output operations, like header() calls.