Speed Up Your Website With PHP Buffer Flushing

January 24th, 2011

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:

1. the PHP interpreter reaches the end of the page
2. the buffer exceeds the number of bytes specified within PHP’s output_buffering configuration setting, or
3. 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 tag, e.g.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<BODY>
This page is loading.<br />

<?php flush(); sleep(2); ?>
Almost there…<br />
<?php flush(); sleep(2); ?>
Done.<br />
</BODY>
</HTML>

(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 body content.

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.

Tags: , ,

2 Responses to “Speed Up Your Website With PHP Buffer Flushing”

  1. Modern versions of PHP set the output_buffering flag in php.ini. This buffers your HTML output and sends it when your PHP code has been processed or once the buffer reaches a limit (e.g. 4,096 bytes). You can also use PHP’s ob_start() and ob_end_flush() to implement your own buffering functionality.

    If output buffering is enabled, you can set HTTP headers and cookies after outputting HTML because returned code is not sent to the browser immediately.

    Note that older versions of PHP and some ISPs do not enable output buffering — it hits server performance and requires more memory. Even if you’re certain buffering is always enabled, it’s good practice to set HTTP headers and cookies before sending page content.

  2. Deep Fryers says:

    Your Friend & Partner…

    Hey – great blog! I’m interested in becoming partners – please go to my website and look under “Resources” to become our partner – it would mean a lot! Thanks a ton…