soma56 Posted June 9, 2010 Share Posted June 9, 2010 I'm new to PHP and for example purposes I've included this simple code: $i = 1; while ($i <= 10) { echo $i++; } My question is simple. As my code is a little more complex it seems to load until the loop is true before "echoing" anything. Is there a function that allows me to "echo" each case as it's being created? My code seems to take "a while" to load and then echoes back everything at once. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/ Share on other sites More sharing options...
sspoke Posted June 9, 2010 Share Posted June 9, 2010 while loops run while everything is true unless you do while(!something) { ... } then you will keep looping falses until something is actually turns into true.. then it will stop! Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069732 Share on other sites More sharing options...
trq Posted June 9, 2010 Share Posted June 9, 2010 The web doesn't really work like that. Your response is generally built and then sent. You can muck around with output buffering but its pretty unreliable at best. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069733 Share on other sites More sharing options...
soma56 Posted June 9, 2010 Author Share Posted June 9, 2010 Thank you for both of your responses. I will re-code to see if I can get the results I'm looking for. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069756 Share on other sites More sharing options...
micah1701 Posted June 9, 2010 Share Posted June 9, 2010 the point thorpe was touching on is "Sever-side" verse "Client-side" scripts. PHP runs on the server and the entire script is executed before its data is sent to the client-side, aka your browser. If you want to have things happen on your browser's screen after the page has loaded from the server, you need to use a client-side script just as javascript. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069922 Share on other sites More sharing options...
Daniel0 Posted June 9, 2010 Share Posted June 9, 2010 It is possible doing something like it, assuming your web server is not setup to always buffer the output and that your browser doesn't try to load the entire thing first. <?php header('Content-type: text/plain'); $i = 1; while ($i <= 100) { echo $i++ . PHP_EOL; flush(); ob_flush(); usleep(500000); } Live example: http://files.degeberg.com/flush.php This works for me in Firefox 3.6.3, but not in Chrome 6.0.422.0 dev. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069952 Share on other sites More sharing options...
Mchl Posted June 9, 2010 Share Posted June 9, 2010 On the other hand, if you run your code from the first post using command line, you will see output appearing as you were expecting it. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069973 Share on other sites More sharing options...
soma56 Posted June 9, 2010 Author Share Posted June 9, 2010 @Daniel0 Thank You:) You made my day. It worked flawlessly. Being new to PHP I really appreciate everyones insight. Thanks Everyone. Quote Link to comment https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1070023 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.