I need to output FIRST first, then sleep and then LAST.

echo "FIRST: " . date("h:i:s") . "<br />";
sleep(3);
echo "LAST: " . date("h:i:s");

But the problem is both FIRST and LAST are displayed after sleep().

I've also tried flush() but nothing seems to be working.

Is there any way to fix this?

Thanks

    echo "FIRST: " . date("h:i:s") . "<br />";
    usleep(3000000);
    echo "LAST: " . date("h:i:s"); 
    

    where you delay execution time in milliseconds

      samuelcook;10982591 wrote:
      echo "FIRST: " . date("h:i:s") . "<br />";
      usleep(3000000);
      echo "LAST: " . date("h:i:s"); 
      

      where you delay execution time in milliseconds

      Nope! Milliseconds is not what I'm trying to do if you read again. It produces the same result.

        Will not necessarily work on all web hosts (will probably never work on a Windows host), as it depends on how they do their buffering, but...

        <?php
        echo "First<br />";
        ob_flush();
        flush();
        sleep(3);
        echo "Last";
        

        Also, browsers may do their own buffering, in which case you may have to add a lot of padding to the above before the browser would actually render the first echo, even if the web server does not override the flushing of the buffer.

        If you need a more portable solution, you may have to depend on the client to handle it by using some JavaScript.

          NogDog;10982596 wrote:

          Will not necessarily work on all web hosts (will probably never work on a Windows host), as it depends on how they do their buffering, but...

          <?php
          echo "First<br />";
          ob_flush();
          flush();
          sleep(3);
          echo "Last";
          

          Also, browsers may do their own buffering, in which case you may have to add a lot of padding to the above before the browser would actually render the first echo, even if the web server does not override the flushing of the buffer.

          If you need a more portable solution, you may have to depend on the client to handle it by using some JavaScript.

          Ohh I see. I'm on Windows, maybe that's why it's not working. Also, I believe it's better not to use codes that are not consistent with cross browsers. I guess I'd have to switch to javascript for the task.

          Thank you so much.

            Write a Reply...