Speed Up Your Website With PHP Buffer FlushingBy Craig Buckler
PHP output buffering is normally enabled by default. In older versions of PHP, a string would be sent to your browser every time the interpreter encountered an echo statement or text outside the PHP delimiters.
Output buffering makes this process quicker and more efficient. The buffer is essentially a big memory-resident string. When text is output, it’s appended to the buffer rather than returned to the browser immediately. The buffer is then “flushed”, i.e. its contents are transmitted and the string is reset pending further output. Flushing occurs when:
- the PHP interpreter reaches the end of the page
- the buffer exceeds the number of bytes specified within PHP’s output_buffering configuration setting, or
- the flush() or ob_flush() functions are called.
There are a few caveats but, assuming it works within your environment, you should consider flushing the buffer immediately after the page’s </head> tag, e.g.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Buffer flushing in action</title> <link rel="stylesheet" type="text/css" href="styles.css" /> <link rel="shortcut icon" href="favicon.ico" /> </head> <?php // flush the buffer flush(); ?> <body> …
(If you’re using WordPress, you could use similar code in your theme’s header.php file.)
Once the browser has received the HTML
head, it can begin to download all linked CSS files, favicons and other resources. These downloads can occur while the browser is idle and waiting for the main
The speed increase will depend on the server-side processing required, the weight of your page, the quantity and size of your CSS files, and whether the browser has cached any resources. However, it’s such a simple optimization, there’s little reason not to do it.
I’d be interested to know whether this technique results in a perceivable speed difference on your website or application.
SitePoint WordPress Restaurant Theme
SitePoint WordPress Ecommerce Theme
SitePoint WordPress Portfolio Theme
Responsive Web Development
Elm: A Beginners' Guide to Elm and Data
Jump Start Responsive Web Design, 2nd Edition
Designing UX: Prototyping
Researching UX: Analytics
- 1 Phpseclib: Securely Communicating with Remote Servers via PHP
- 2 Build Your Own Dropbox Client with the Dropbox API
- 3 Is It Possible to Write and Run PHP Code on an iPad?
- 4 How to Improve User Experience with Customer Journey Optimization
- 5 Picking the Brains of Your Customers with Microsoft's Text Analytics