0

Wonder if there's a way for a PHP script returns and print immediately on the browser (via a web page) the result of various functions without waiting for the completion of the next.

Sample PHP code:

<? php
for ($i = 0; $i < 100; $i++) {
    echo $i. ":". my_function(); / / takes a long time to run
}
?>

release:

0: value ... (the function of the cycle $i = 1 is still running)
1: value ... (the function of the cycle $i = 2 is still running)

and so on ...

dceccon
  • 423
  • 1
  • 7
  • 25
  • No. PHP has to calculate everything before it can render the HTML. Theres no way to create something like that in PHP dynamically. – Realitätsverlust Jan 16 '14 at 10:16
  • 1
    @YUNOWORK Not true. PHP doesn't have to wait for anything, it just outputs values as it goes. The *web server* is typically the one buffering the response. – deceze Jan 16 '14 at 10:17
  • OP: HTTP is _synchronous_ and any output will be sent to client only after script was executed. It's not like CLI-mode at all – Alma Do Jan 16 '14 at 10:18
  • @deceze hm ok, didnt know that. Ill retreat in shame. :c – Realitätsverlust Jan 16 '14 at 10:20
  • Maybe you could potentially write to a file in each function the returned answer. Then look at this http://stackoverflow.com/questions/4078352/web-implementation-of-tail-f-filename. Where you could potentially tail that file on a web page. This is about the only thing I can think of. Some more links https://code.google.com/p/php-tail/, http://stackoverflow.com/questions/1102229/how-to-watch-a-file-write-in-php – The Humble Rat Jan 16 '14 at 10:22
  • @TheHumbleRatc mmmm, ok because the php-tail example use a dynamic invocation of php function via jquery script. It's an idea, thx :) – dceccon Jan 16 '14 at 10:35

1 Answers1

1

you may use the flush() function to attempt to flush data back to the browser.

description from php.net:

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

sample code below, tested on chrome 31.0.1650.57 (linux), Safari (6.0.4) (osx):

note: whether or not to show the flushed output is up to the browser (usually depending on the amount of data in the response). for example Safari (6.0.4) wants 512 bytes of data before outputting data to the browser. You can get around this by doing something like padding 512 characters to beginning of the output.

<?php
header( 'Content-type: text/html; charset=utf-8' );
echo str_repeat(" ",512); //pad the buffer with data (in case browser needs it)
while (true){
    //write go to the browser every 2 seconds.. forever
    echo "go...<br/>";
    ob_flush(); 
    flush(); 
    sleep(2);
}
?>

more info here http://us1.php.net/manual/en/function.flush.php

KorreyD
  • 1,274
  • 8
  • 15
  • 1
    I'd appreciate if you would please provide a constructive reason as to why this was downvoted. – KorreyD Jan 16 '14 at 11:25