为什么PHP会一次性发送所有内容? [重复]

Possible Duplicate:
what is output buffering?

I have some content that takes a while to be generated by PHP. Meanwhile I would like to use this simple JS trick to display a "Loading..." message. The message would disappear as soon as the output is generated.

<p id="loading_msg">Loading...</p>

<h1>Hello</h1>
<?php
    sleep(2); // This is the greedy function call
    print '<p>This content definitely took a while to be generated !.</p>';
?>

<script type="text/javascript">
    var e = document.getElementById("loading_msg");
    e.style.display = "none";
</script>

The problem is, instead of sending everything up to sleep(2); then blocking, it will wait before all the page is generated, and then, send it all at once. It obviously breaks the purpose of my "Loading..." message, because it doesn't appear before the 2 seconds have elapsed. Any ideas of why, and how I could work around this ?

By default, PHP will buffer your output and send a fewer number of larger chunks to the user. You can use flush to send the contents of write buffer to the user immediately:

<p id="loading_msg">Loading...</p>

<h1>Hello</h1>
<?php
    flush();
    sleep(2);

This won't work if you've previously enabled output buffering by calling ob_start or the php.ini directive output_buffering to 1.

Using 'flush' might work on some webservers / browsers (meagar is correct that it won't work while output buffering is enabled - but that doesn't mean it will work the rest of the time).

It's up to the webserver to decide when to revert to chunked encoding - most will start a chunked response when you call flush as long as you're not compressing the output stream. Then it's up to the browser to decide when it starts rendering an incomplete response - and behaviour will vary depending on the content too - e.g. none will start rendering a table until it has the closing tag.

A better solution then trying to handle it in the receiving page, is to handle it in the sending page - e.g. using a litebox to display the message. Alternatively the message could be presented in an interim page which does a redirect to the generated content.