Why doesn't echo work in a do while loop or even when ignore_user_abort (1)?

I used a log printer to check if ignore_user_abort is working. However, the echo does not want to work. Will the echo only work when the loop ends?

<?php ignore_user_abort(1); // run script in background set_time_limit(0); // run script forever $interval=2; // do every 2 sec... $i=0; $lastRunLog = 'lastrun.log'; do{ // add the script that has to be ran every 2 sec here // ... echo 'Test: '.$i; file_put_contents($lastRunLog, time()); sleep($interval); // wait 2 sec $i=$i+1; }while(true); ?> 
+4
source share
1 answer

Yes and no.

There are three output buffers you need to worry about:

  • PHP can output a buffer. You can use the ob_ functions to manage this buffer and force the exit to apache / iis (web server).
  • Apache / IIS have their own buffers and will only send information when they consider that they are enough to send. It is also complemented by mod_gzip or other compression. You cannot control this through PHP.
  • There are also potentially other caches / proxies between the browser and the server. They MAY hold the exit until he gets the full answer.

So echo immediately puts it in the PHP buffer. You can send the buffer to the web server, but after that it is out of your hands.

+1
source

All Articles