Jump to content

while ($i <= 100)


soma56

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/204245-while-i/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069922
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/204245-while-i/#findComment-1069952
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.