2

I'm trying to display realtime php process output using this code below, but it doesn't work, it doesn't show the process in realtime, it only displays the final output at once

echo "Start...";

for ($i = 0; $i<20; $i++){
    echo $i;
    ob_flush();
    flush();
    sleep(1);
 }

echo "Done!";
ob_end_flush();
exit;
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Mohamed
  • 123
  • 1
  • 6

3 Answers3

0

Flush is on the PHP side. However, some webservers have their own output cache.


The IIS for example has the ResponseBufferLimit option.

%windir%\system32\inetsrv\appcmd.exe set config /section:handlers "/[name='PHP_via_FastCGI'].ResponseBufferLimit:0"
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
0

Most browsers nowdays wont display the page until they have all the initial DOM content (excluding separate resources like images etc) unless the page is very large. I think it is to ensure all the css rules are present before displaying anything. Heres what the manual says

Bug
  • 508
  • 1
  • 3
  • 15
0

Your question wasn't clear. I thought you were running PHP directly. If you're running PHP through a browser this wouldn't work. PHP through a browser would run the entire program and then server the page. It doesn't have any facilities for updating the HTML. You typically use ajax to do that.

I assume your code snippet actually looked like this:

<!DOCTYPE html>
<html>
<body>

<?php
   echo "My first PHP script!";
?>

</body>
</html> 

If you want to do this in PHP, you have to figure out how to run it directly. Try https://stackoverflow.com/a/9520125/5276890.

Community
  • 1
  • 1
Roy Falk
  • 1,685
  • 3
  • 19
  • 45