in reply to printing loops on the fly

If the page has tables, that might be the problem. Most browses don't display tables until the table is finished. Make sure the page does not have table tag that contains lot of data.

Update: Also make sure that the list in parenthesis after the foreach does not take long to calculate. If it does, transform it to some iterating form. For example, change for $line (<FILE>) {...} to while ($line=<FILE>) {...}; etc.