Using the PHP Buffering Functions
PHP output buffering is a feature that allows you to store HTML content in a buffer until it can be sent to browsers. This can improve performance by allowing PHP to send the HTML in chunks rather than all at once. PHP provides a set of functions to control the buffering: ob_start, ob_get_contents, and ob_flush. These function all work with the same basic principle, they create a buffer into which data is stored and can be accessed by PHP scripts. A boolean value is used to specify whether the function can clean, flush, or delete the buffer, and an optional bitmask can be used to restrict the operations that are permitted.
The ob_flush function pushes the current output to the browser and also closes the buffer. This will not override the web server's buffering schemes, and it also doesn't affect any client-side buffering that might be in place.
ob_get_contents() is a function that can be used to get a copy of the contents of the buffer. This can be useful for things like GD-based image manipulation and other uses of a server-side cache. Note that calling ob_get_contents() in a callback function may cause the script to change its working directory, which could cause problems for some web servers, such as Apache. It is recommended that you use chdir($_SERVER['SCRIPT_FILENAME']) in the callback function to avoid this issue.
ob_end_clean() is another function that will close the buffer, but it does not clear what is already in it. This function is the one that is recommended to be called at the end of your script, and it can be used in conjunction with ob_get_contents().