daydreamer Posted April 18, 2009 Share Posted April 18, 2009 How do I output to the browser before the script finishes executing? For example, the code below will output all 100 $outputs at once, how do I make it so it will output as soon as that section of code is read/processed? <?php for ($i = 0; $i <= 100; $i++) { sleep(3);//I have some code which takes about 3 seconds to execute echo $output; } ?> Thanks. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted April 18, 2009 Share Posted April 18, 2009 PHP works server side, so it does it ALL before it outputs to the browser... Quote Link to comment Share on other sites More sharing options...
daydreamer Posted April 18, 2009 Author Share Posted April 18, 2009 what about flush? http://uk3.php.net/flush Quote Link to comment Share on other sites More sharing options...
Axeia Posted April 18, 2009 Share Posted April 18, 2009 Yahoo's best practices recommend using flush right after the </head> of your page to speed up page loading. Quote Link to comment Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 18, 2009 Share Posted April 18, 2009 like daydreamer say try flush() if it don't work look for mod_gzip or mod_deflate module, compression module always buffered output before compressing them and send them, turn that off. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2009 Share Posted April 18, 2009 Getting this to work is dependent on web server settings, php settings, and browser type and browser settings. While you might get it to work on your local development system or a dedicated server, as soon as you try it on a server that is not fully under your control, it has a chance of not working and you won't be able to get it to work because you won't have the ability to change the server and php settings that affect it. Web servers and browsers are not designed to have the output from one http request be incrementally output and have it rendered in the browser in real time. To get this to work for all web server/php configurations and settings you must make a http request for each piece of output by either having the browser continually refresh the page or use AJAX. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 18, 2009 Share Posted April 18, 2009 Could you not just do this? <?php ob_start(); for ($i = 0; $i <= 100; $i++) { sleep(3);//I have some code which takes about 3 seconds to execute echo $output; echo ob_get_contents(); } ob_end_clean(); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 19, 2009 Share Posted April 19, 2009 If the output from an echo is not being sent and rendered by the browser, echoing the content of the output buffer is not going to bypass all the buffering, compression, and minimum length content requirements that the web server, php, and the browser are imposing on the content either. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.