How to PHP Flush Buffer Early to Speed Website Loading

By Posted 2011 Updated   BloggingTutorials

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.


11 comments on “How to PHP Flush Buffer Early to Speed Website Loading

  1. Saket Jajodia says:

    Going to give a try on my blog and will see will it help me or not.. Thanks for this code.. :)

  2. Pothi says:

    When we use caching plugins (such as WP Super Cache that is also used in this blog), I don’t think this will have any effect. But this is an interesting idea. I guess if anyone uses this, it should be used just after wp_head() function.

    • QuickOnlineTips says:

      wp head position can vary in the HEAD in custom themes, so scripts and CSS may actually be after it.

  3. Rakesh Kumar says:

    Generally i used cache plug-in but this time i really need work on it. Thanks

  4. Maxim says:

    This looks like a great tip for php developers.. Is there a substitute for this in java?

  5. Nasif says:

    How will I add it in thesis theme ?

  6. Joel says:

    @Maxim: The substitute in Java is response.getOutputStream().flush();

    Regards.

  7. budyk_ir says:

    great. doesnt the PHP flush it automatically ?

Leave a Reply

Your email address will not be published. Required fields are marked *




css.php