Skip to content Skip to sidebar Skip to footer

When Does Php Not Output The Page All At Once?

Here's something odd, I though this would output a page and show part by part, until all is loaded (similar to how Wordpress update/reinstall process works):

Solution 1:

I dont know how you think php works, but it is only server side and you are expecting partial updates in client side. PHP does not work like that.

When you call that php script, php interpreter begin to execute it, putting the output into temporary location, then with the sleep function, php interpreter waits until milliseconds are reached, then continue executing, concatenating the output in the temp location, and go on... after all script execution is done, php sends the whole output to the client.

If you need partial updates on a page, you need to enter in the world of async calls and ajax.

Solution 2:

It buffers the output and only send it when the page is finished loading or, afaik, after a few kilobytes were buffered. You can control this functionality by either completely wrapping your code in ob_start() and ob_end_flush(), or in this case, calling flush() before every sleep.

More info: What is output buffering?

Solution 3:

php is server side. Have you tried this with javascript's setTimeout() function?

Here's an example of a string that's repeated 10 times after a 5 second delay.

<script>setTimeout(test, 5000);

functiontest(){
    for (var i=0;i<10;i++){
    document.write("test <br />");
    }
}
</script>

Post a Comment for "When Does Php Not Output The Page All At Once?"