0

I'm trying to echo after every iteration of a foreach loop in PHP. The echos aren't rendering until they are all done. I've tried the suggestions from various posts but none are working. Some of my code is:

echo $epTitle . "<br>";
echo "<div>" . '<img src="' . $imgSrc . '" height="70" width="70">';
echo "<button onclick='grabVid(" . '"' . $pageUrl . '"' ..")'>Grab</button>";

I've tried the following:

flush();

.

flush();
ob_flush();

.

while (ob_get_level() > 0)
ob_end_flush();

.

@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
@ob_end_clean();

and other suggestions from: http://php.net/manual/en/function.flush.php and Echo 'string' while every long loop iteration (flush() not working)

Any suggestions? Thanks.

user3080392
  • 1,194
  • 5
  • 18
  • 35
  • The only way I've accomplished this succesfully has been using Ajax requests. Otherwise the PHP script will process all and then send the output back. – MarkSkayff Mar 22 '18 at 23:39
  • @ MarkSkayff I'm using Ajax post method which posts to the above script. It still doesn't echo out after each iteration. – user3080392 Mar 22 '18 at 23:51
  • Because that isn't how HTTP normally works. Web servers send headers then a response body. Nothing is sent until the request is done so the proper headers are sent (HTTP status code, content type, content length). – Devon Bessemer Mar 22 '18 at 23:54

2 Answers2

1

If you're using XHR (AJAX) and want to stream the response, you need to periodically check on it using onprogress. The onreadystatechange event will not fire whilst the response is being received, because the readyState will always be loading and not change until the request has completed.

Here is a good article on the subject:

PHP Streaming and Output Buffering explained

It contains this sample JavaScript for the XHR part:

xhr = new XMLHttpRequest();
xhr.open("GET", "response.php", true);
xhr.onprogress = function(e) {
  alert(e.currentTarget.responseText);
}
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    console.log("Complete = " + xhr.responseText);
  }
}
xhr.send();

(I don't know if there's any reason why the example uses e.currentTarget instead of xhr inside the onprogress event handler - I just copied the example as it is.)

If you're planning on doing this loop for longer than 30 seconds, your PHP script will probably be killed. You can extend the default time limit but if you're site has many users, you want requests to be short as possible for optimum server availability.

This means rewriting your loop so that each iteration is a single HTTP request.

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
0

Using ob_flush() right after the echo, worked for me.

Pablo Camara
  • 622
  • 1
  • 5
  • 14