1

I hope that you can help me.

I have a very simple problem :-)

$i = 1;
while ($i < 5)
{
    echo $i . '<br />';
    sleep(1);

    $i++;
}

When I run this code, it waits the couple of seconds as it should, and then displays the echo all together. How do I get it to display 1, then wait a second then 2, etc and not all at once at the end of the loop?

Could you please help with an example?

Thank you for your help

Kind Regards

A31Llama
  • 11
  • 3
  • 2
    you're probably running this under a webserver, which means output can be cached and then sent as a big "blob". try adding `flush()` calls. but note that you can NOT do anything about any other caches beyond the immediate webserver one. in general you can NOT do this kind of "timed" output while in a web context. – Marc B Nov 01 '16 at 14:23
  • in that way, its simply not possible... you got two options: 1st: reload the page every time to show the changes 2nd: use ajax – FalcoB Nov 01 '16 at 14:24
  • Have you tried running that on CLI (Command Line Interface)? – barudo Nov 01 '16 at 14:27
  • web servers and HTML clients don't normally work this way. Web pages are static, and aren't rendered by your browser until it's gotten all the code sent from the server. You might be able to do what you want with javascript though – Duane Lortie Nov 01 '16 at 14:31
  • Ugh I am the one supporting JS. You can get the output in the client you just need to use a client language ie JavaScript. – nerdlyist Nov 01 '16 at 14:36
  • It seems you are looking for something like this [here](http://stackoverflow.com/questions/15036232/php-loop-how-to-print-each-result-and-delay-it-for-a-second-before-echoing-anot) – Just_Do_It Nov 01 '16 at 15:31

1 Answers1

0

First we have to understand how the life-cicle of a web-app works:

  1. Webserver gets a request
  2. PHP executes the script you called
  3. Body and Headers of the response are generated
  4. Webserver returns the response to the client

As you can see, there is only a request and a response. You should not recieve the response stepwise and start rendering before you have the entire response.

Unfortunely, there's no simple way of archieving this reliably. The best way of solving your problem is setting up a real-time-comunication layer. You might have heard of "websockets".

  1. Setup a websocket-server on your server
  2. Now you can connect to it from PHP and start throwing messages into a pipeline
  3. Setup a websocket-client in your javascript and connect to your socket-server pipeline
  4. Everytime the socket-client recieves the message, fire an event and do you DOM transformation clientside.

You could also use Pusher or something similar. This way you do not have to worry about setting up your own websocket-server. But you can use their platform and their client library.

Hope it helps.

Patrick
  • 203
  • 1
  • 9
  • eh.. hoped it would be something small I missed :-) But thank you guys very much for your answers. It helps. Of to Googling JavaScript then :-) Thank you – A31Llama Nov 02 '16 at 05:58