0

I am trying to implement a simple while-loop on my WAMP server by having a string iterate a certain number of times. However, the entire output occurs at once, despite turning off output buffering in the WAMP PHP settings.

Version 1

$i = 0;
while ($i < 5)
{
print ("This is an example of a while loop.<br/>");
flush();
sleep(1);
$i++;

}

Version 2

$i = 0;
while ($i < 5)
{
print ("This is an example of a while loop.<br/>");
ob_start();
ob_flush();
flush();
sleep(1);
$i++;

}

Neither version outputs the string the way I am intending, namely, one at a time at one second intervals. Any help is greatly appreciated.

dmubu
  • 1,073
  • 4
  • 16
  • 18

3 Answers3

0

I've always had issues with WAMP and flush, and eventually came to the conclusion that it's simply broken in WAMP. It seems no matter what server settings I have, there's something about the way WAMP is packaged, that just won't work.

The only way you're going to get it to work is by using XAMPP, or installing and configuring your own server.

Shea
  • 1,965
  • 2
  • 18
  • 42
0

I had this problem and was able to solve it by incorporating the following bits of code.

// Necessary Settings and stuff for output buffering to work right
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ini_set('output_buffering', "off");

// Start a new output buffer and send some blank data to trick browsers
ob_start();
echo str_repeat(" ", 4096);

for ($i=0; $i < 10; $i++) {
    echo "<div>Echo Something...</div>\n";

    // Call both ob_flush and flush functions
    ob_flush();
    flush();
}
0

For those who cannot make it even with Justin's workaround. Tested on wamp x64.

// Necessary Settings and stuff for output buffering to work right
apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ini_set('output_buffering', "off");

// Start a new output buffer and send some blank data to trick browsers
ob_start();
echo str_repeat(" ", 4096);
ob_end_flush(); //addition
ob_flush();
flush();

for ($i=0; $i < 10; $i++) {
    ob_start(); //addition
    echo "<div>Echo Something...</div>\n";

    ob_end_flush(); //addition
    ob_flush();
    flush();
}