0

Firstly, I need to do some job and write "echo result", then, wait 10 seconds then do sth.

Basic example:

// Now
echo date('h:i:s') . "\n";

// wait for 2 sec.
sleep(2);

echo date('h:i:s') . "\n";

PHP waits around 2 seconds writing all echo commands. But it must write first echo print; then, wait for 2 sec. and then, write second one date. How to solve this? Am I incorrect?

How to achieve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jack UK
  • 45
  • 5
  • Can you add your current output to your question? For me it seems ok when I copy and paste your code. – Christoph Kluge Feb 23 '20 at 12:59
  • 1
    I think you need to add `ob_flush(); flush();` after the **first** `echo` – Alon Eitan Feb 23 '20 at 13:01
  • 1
    @ChristophKluge , output is okay. The problem is that PHP waits for 2 seconds even first "echo". When you run the script; PHP, will waits for 2 seconds, then, write all prints together. Normally, **it should write first "echo" directly**, then, wait for 2 seconds and then, write the second echo. – Jack UK Feb 23 '20 at 19:10

2 Answers2

0

If PHP is running via a webserver, what you ask is impossible. PHP does not control how the webserver buffers data. The flush() only affects the PHP buffers - whether the webserver sends stuff out via HTTP is its decision.

Further, even if the webserver does send the content, the browser then decides when to render it. The logic around progressive rendering in browsers is rather complex.

OTOH the CLI SAPI will send the content immediately after flush()

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 1
    "@symcbean, Yes PHP is running via a web server (Apache). and okay it is impossible. How can I do this: When one request comes, I should say, **"return true;", directly. Then, I need to wait 10 seconds to contunie do sth for same request's job.** (If I say, "wait for 10 seconds" then, do this job, and then, "return true"; request connection will fell down, it is other's side problem.) Do you have any ideas about this? – Jack UK Feb 23 '20 at 19:09
  • 1
    Javascript setTimeout() – symcbean Feb 23 '20 at 19:50
0

thank you for answering the questions inside the comments. So as far as I understand you're running your scirpt behind a webserver.

I would like to point out that it's possible. The magic key here is the HTTP header Transfer-Encoding: chunked. When Apache does not know the Content-Length then it will start streaming the response in chunks to the client/browser. With this in mind it should be possible to configure Apache and PHP to support this.

Here is a referencing question which is worth reading to understand the topic: How to make PHP generate Chunked response

Another solution could be to take a look at something like https://reactphp.org. I've used it in the past, contributed to it and know it will solve your task as well but it will lead to infrastructural changes.

Hope this helps you. Please let me know if you have further questions around this topic.

Christoph Kluge
  • 1,947
  • 8
  • 23