4

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):

<html>
<body>
<?php
for( $i=0; $i<100; $i++)
{
    echo 'HELLO';
}
sleep(10);
echo '<p></p>';

for( $i=0; $i<100; $i++)
{
    echo 'THERE';
}
sleep(10);
echo '<p></p>';

for( $i=0; $i<100; $i++)
{
    echo 'HOW ';
}
sleep(10);
echo '<p></p>';


for( $i=0; $i<100; $i++)
{
    echo 'ARE U';
}
sleep(10);
echo '<p></p>';

Oddly enough it waits for the entire page, then shows it. What variables/configuration affect this behavior?

NoBugs
  • 9,310
  • 13
  • 80
  • 146

3 Answers3

6

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.

Ivan Perales M.
  • 347
  • 4
  • 11
5

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?

Anonymous
  • 11,740
  • 3
  • 40
  • 50
1

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);

function test(){
    for (var i=0;i<10;i++){
    document.write("test <br />");
    }
}
</script>
Felix
  • 65
  • 1
  • 7
  • That's not related to the problem I'm trying to solve regarding why the entire page is printed out at once. – NoBugs May 06 '16 at 01:50
  • php is server sided, which means that it gets sent to the server all at once, gets processed, and then returns the result. Doing something dynamic like this would be possible javascript. – Felix May 06 '16 at 02:04
  • With a score over 3000, I think OP understands what PHP is. They're just learning the quirks that make up 90% of this language. – Anonymous May 06 '16 at 02:15
  • That's not a "quirk" of PHP. It's part of the HTTP protocol. Recall that the protocol requires that you define the response size (Content-Length) in the HTTP headers and it's impossible for any language to anticipate it's response size until it's done processing all potential output. – Sean May 06 '16 at 02:20
  • @Sean Good point, but I *know* I have seen various pages that start loading, then expand as the page fully downloads. No page (except perhaps a very very small one) comes over in just one packet! – NoBugs May 06 '16 at 04:49
  • Whelp, the headers probably don't fit in one packet. Can you cite an example because I'm fairly certain Felix's answer is the correct one? And that what you've seen is a progressive download of information from JavaScript. Unless what you're thinking of is something else like WebSockets or HTTP/2. – Sean May 06 '16 at 12:22