How to PHP Flush Buffer Early to Speed Website Loading

Did you know you can use PHP flush function to flush buffer early on your server to speed site loading.  I was reading this amazing Yahoo! guide about best practices for making web pages load faster, and though most other tips were common and practiced by most webmasters for faster site loading, this was new to me.

How does PHP Flush work?

When a page is requested on your server,  it takes 200-500ms for the backend server to  stitch the HTML page. While the  browser is idle as it waits, you can use PHP function flush() which allows you to send your partially ready HTML response to the browser so that the browser can start fetching components while your backend is busy with the rest of the HTML page. The benefit is mainly seen on busy backends or light frontends. Learn more about PHP flush() function, its uses and limitations.

Where to Add PHP Flush in HTML

They recommend to add it right after the HEAD tags and before the BODY tags on your html page. This allows browser to fetch CSS and JavaScript files in parallel while the backend is still processing.

 </head>
    <?php flush(); ?>
<body>

Have you tried PHP flush on your server? Have you noticed a difference in site speed? Every millisecond counts, especially since site speed is a Google search engine ranking factor now.

Share with friends

About the Author: P Chandra is editor of QOT, one of India's earliest tech bloggers since 2004. A tech enthusiast with expertise in coding, WordPress, web tools, SEO and DIY hacks.