Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Flush: How Often and Best Practices

Tags:

I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after the top portion of my page loads (head, css, top banner/search/nav).

Is there any performance hit in flushing? Is there such a thing as doing it too often? What are the best practices?

If I am going to hit an external API for data, would it make sense to flush before hand so that the user isn't waiting on that data to come back, and can at least get some data before hand?

like image 940
Cory Dee Avatar asked Dec 09 '08 13:12

Cory Dee


People also ask

What is the purpose of flush in PHP?

The flush() function requests the server to send its currently buffered output to the browser. The server configuration may not always allow this to happen.

What is output buffering in PHP?

Output Buffering is a method to tell the PHP engine to hold the output data before sending it to the browser.


2 Answers

The technique described looks nice, but has several pitfalls:

1) the time between PHP script start and end is small compared to transmission time; also, this saves the user about 0.5 seconds, according to your source. Is that a significant amount of time for you?

2) this technique doesn't work with gzip output buffering

3) if you flush too often, you'll be sending an almost-empty packet on flush, which might actually increase loading time (on slow, noisy connections).

4) once you flush, you can't send any more headers

5) (minor issue) the server response will come in chunked encoding, which means the client won't know the size in advance (therefore won't display "x% done" when downloading a file).

On the other hand, if you expect your script to run for a loooong time (20+ seconds), it may be needed to send some data (spaces, for example) to keep the browser from timing out the connection.

like image 166
Piskvor left the building Avatar answered Sep 18 '22 13:09

Piskvor left the building


Down side is that you can't gzip the content as well as flushing it afaik, so I've always preferred to gzip rather than flush.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

This makes this not idea, as it seems padding more data isn't very useful.

like image 28
Rich Bradshaw Avatar answered Sep 19 '22 13:09

Rich Bradshaw