• Home
  • About
  • Who Am I?
  •  

    Force output to browser in PHP

    During the past i have had some serious issues with scripts that were supposed to run for quite some time and getting them to show output as the script proceeds

    for instance lets take this code for example:

    for($i=1 ; $i < 100 ; $i++){
    echo i.”<br>”;
    sleep(1);
    }

    Now the above code should print $i as it proceeds but most of the time it doesnt happen that way. So what i found after some research is .. put the code like this.

    ob_end_flush();
    for($i=1 ; $i < 100 ; $i++){
    echo i.”<br>”;
    @ob_fllush();
    flush();
    sleep(1);
    }

    Also some times the httpd.conf file includes instructions to turn on gzip compression on all or selected sites. In this case the script wont output untill it reaches the end , simply because the gzip compression takes place first on the script output and then the compressed data is sent to the bowser for decoding. I would suggest turning off compression, this will surely help :)


    One Response to “Force output to browser in PHP”

    1. Craig Sparks Says:

      Your code has an error, ob_fllush() should be ob_flush(), but thanks, this should be helpful for something I am working on now.

    Leave a Reply