tags:

views:

79

answers:

3

I'm using a sleep function inside of a foreach loop and I'd like to echo the value inside the loop. Why isn't this working? The $test var inside the loop never changes from 0.

foreach($test as $val){
 ob_start();
 echo $test++;
 sleep(1);
 ob_end_flush();
}
A: 

Flush after each echo, instead of using output buffering.

Ignacio Vazquez-Abrams
Ignacio, I tried flush and got nothing. The same thing happens. I try to echo anything inside the loop and it just hangs.
jim
A: 

ob_flush goes outside the loop and not in. Now it is working as expected. Thanks guys.

jim
You might want to accept an answer.
Chacha102
+2  A: 

This works:

foreach ($test as $val)
{
    ob_start();

    echo $val++;

    sleep(1);

    while (ob_get_level() > 0)
    {
        ob_end_flush();
    }

    flush();
}
Alix Axel