Why to wait for the response from the server, if we can render the frontend script in between. Here, the Flush method comes into picture.
Did you ever observe that some websites take a fraction of seconds to start displaying the content, whereas few websites, such as Google or Facebook start displaying the content like a flash?
This happens when a dynamic page is waiting for the server's response to display the HTML page. During that time browsers remain idle, which you can observe easily. We can use the flush method to download all CSS, JavaScript, and other content mentioned in the header and the browser can start displaying the partial content of the body part at the same time, so the user will feel that the site is a bit faster. This trick is beneficial for those websites where lots of backend requests are sent or frontend is having less content to display.
A good place to consider flushing is right after the head, because the HTML for the head is usually easier to produce and it allows you to include any CSS and JavaScript files for the browser to start fetching in parallel, while the backend is still processing. For example:
PHP: <?php flush(); ?>
It is not much effective though, but considering this fact one should consider flushing the buffer after the </head>
tag, for example:
</head> <?php flush(); //flushed to display content from body part ?> <body>
Although there are lots of conditions to load a web page faster. It depends upon the server's response of how fast it can send you the HTML output and how fast it can send you all the files mentioned under the <head>
tag. So, flushing the buffer before the <body>
tag is a good idea to make the process faster at the server end, which makes the whole process faster and productive.
Flushing in PHP usually may happen in the following circumstances:
If the PHP interpreter found the end of the page
If the buffer exceeds the number of bytes specified under the PHP configuration setting
If the PHP's flush-related functions are called, such as
flush()
andob_flush()
In any programming language, the concept of flushing the buffer is the same and that is writing the contents of reserved areas of memory to the hard disk. This way you will achieve faster response without breaking anything.