Search code examples
phpoutput-buffering

Difference between ob_clean and ob_flush?


What's the difference between ob_clean() and ob_flush()?

Also what's the difference between ob_end_clean() and ob_end_flush()? I know ob_get_clean() and ob_get_flush() both get the contents and end output buffering.


Solution

  • the *_clean variants just empty the buffer, whereas *_flush functions print what is in the buffer (send the contents to the output buffer).

    Example:

    ob_start();
    print "foo";      // This never prints because ob_end_clean just empties
    ob_end_clean();   //    the buffer and never prints or returns anything.
    
    ob_start();
    print "bar";      // This IS printed, but just not right here.
    ob_end_flush();   // It's printed here, because ob_end_flush "prints" what's in
                      // the buffer, rather than returning it
                      //     (unlike the ob_get_* functions)