无法在Windows中使用flush()

We have a ubuntu test server that this simple script works fine on where it outputs a number each second to the browser

<?
    for ($i=0; $i<3; $i++) 
    {
        echo $i.'<br>';
        flush();
        sleep(1);
    }

However on my local development box (windows environment) I cannot get it to work, it outputs it all after 3 seconds.

I am using the same php version (5.3.13) as well as apache (2.2.22) version on my local box as the test server. I have mirrored most of the settings in both php.ini as well as the httpd.conf Here are settings I have set (which are identical to the test server)

output_buffering 0
zlib.output_compression = Off
zlib.output_compression_level = -1

I don't have mod_gzip installed.

I have even tried doing a wamp install on my box, and it didn't work with that either.

I can't imagine it being a client (browser -- firefox 13) issue as I use the exact same browser to view the script on the test server and it works fine

I have tried enabling implicit flush, doing ob_flush, etc. No luck.

Anyone have any ideas on how to get this working?

Just keep in mind what @Wrikken said, this is not recommended.

So I was thinking, everything you're doing seems to be right. I copied your setup and tried. I faced same problem. I executed the script from command line, it worked.

Then I had to do the last test, Wireshark. Following the first couple of packets it was clear that the server is sending everything correctly, so it had to be the browser’s buffer.

So I tried send some data before the loop, well guess what? It worked!

Here you go:

<?php
ini_set('output_buffering','off');
ini_set('zlib.output_compression', 0);
echo str_repeat(" ", 1024), "
";
for($i=0;$i<6;$i++) {
      echo $i."<br />
";
      ob_flush();
      flush();
      sleep(1);
}
?>

I'm not sure about the application you have in mind, but this is clearly not a good idea, I highly recommend that you look into other options.

Update: After a lengthy Google search I found this and this

Because a browser cannot correctly render a page without knowing how to construct the page's characters, most browsers buffer a certain number of bytes before executing any JavaScript or drawing the page

And

To avoid these delays, you should always specify the character encoding as early as possible, for any HTML document larger than 1 KB (1024 bytes, to be precise, which is the maximum buffer limit used by any of the browsers we have tested).

So I tried to send the charset first instead of filling the buffer: (and it worked)

<?php
ini_set('output_buffering','off');
ini_set('zlib.output_compression', 0);
//echo str_repeat(" ", 1024), "
";
header('Content-Type: text/html; charset=iso-8859-1');
//Note that it shoudn't matter which charset you send
for($i=0;$i<6;$i++) {
      echo $i."<br />
";
      ob_flush();
      flush();
      sleep(1);
}
?>

So why was it working with the first server but not the second?

Most likely it's because your first server is sending the charset with the header while the second one isn't.

In your case, however, I'd make the following changes

<?php
ini_set('output_buffering','off');
ini_set('zlib.output_compression', 0);
//Plain text MIME type since you'll use for logging purposes
//and if you run it from CLI, you can ignore the whole header line
header('Content-Type: text/plain; charset=iso-8859-1');
for($i=0;$i<6;$i++) {
      //No need to echo <br /> once you'll run it from CLI
      echo $i."
";
      ob_flush();
      flush();
      sleep(1);
}
?>

I have found a solution for my problem. FOr some reason adding the line

default_charset = "utf-8"

To my php.ini makes it work. I confirmed my removing the line.. flush didn't work. Added it, and it worked. Maybe because it adds extra headers giving enough for the browser buffer.