Axeem
Axeem

Reputation: 670

PHP - Unbuffered While() Loop

I am using php, I see while() loop in php. I want to ask something can we use
while() loop as mysql_unbuffered_query().
This code make good understanding.

<?php
  echo 'While Loop is going to start';
  $i = 0;
  while($i <= 1000){
      echo 'Now number is '.$i;
      $i++;
  }
  echo 'Continue to running script';
?>

What happens when we run this code first of all this code read while loop is going to start
then read the while loop and create the statement to run Now number is 0.....1000
then read Continue to running script And when the code is finished it print out whole data.But I did not want this.
What I want.
First script read the while Loop is going to start and print out then read while loop and
print out Now number is 0 then go Continue to running script and print out But the back end of script while loop still working, My mean both while loop and continue script working at once.
This give you more understanding.
OUTPUT(First time when script running is start)

 1 - While Loop is going to start
 2 - Now number is 0
 3 - Continue to running script

OUTPUT(Script running is continue)

 1 - While Loop is going to start
 2 - Now number is 0
 3 - Now number is 1
 . - ...............
 1000 - Now number is 1000
 1001 - Continue to running script

Might be this is impossible.If yes how I can do that.

But maybe this is possible like mysql_unbuffered_query().
Like when while loop complete one cycle it print out the number and then other one and so on to complete.
OUTPUT(First time when while loop complete one cycle)

1 - While Loop is going to start
2 - Now number is 0

OUTPUT(while loop complete second cycle)

1 - While Loop is going to start
2 - Now number is 0
3 - Now number is 1

If this is possible, Please guide me how can I do that.
Thanks..............

Upvotes: 2

Views: 1061

Answers (3)

Nate from Kalamazoo
Nate from Kalamazoo

Reputation: 436

If you are running this script from the command line, output buffering is always off, and implicit flush is always on (according to https://www.php.net/manual/en/outcontrol.configuration.php ). Because of this, I suspect you are running this PHP within a web server.

There are several PHP INI values which affect buffering, so you may wish to turn off these features:

<?php
    ini_set('zlib.output_compression', 'off');
    ini_set('implicit_flush', 'on');
    ini_set('output_buffering', 'off');
    echo "While Loop is going to start.\n";
    $i = 0;
    while($i <= 10){
        echo "Now number is ".$i."\n<br>\n";
        $i++;
        sleep(1);
    }
    echo "\n<br>Finished running script\n\n";
?>

Even after all that, it's possible that PHP is behaving exactly as you want, but your web server is buffering the output. This thread may be of interest: php flush not working

If you are using apache, you can add a line to an .htaccess file to turn off gzip compression (which automatically buffers until there's enough data to compress and send) I found this page (http://www.bluehostforum.com/showthread.php?18996-Turning-off-Gzip) which describes the process, but it basically says to add

SetEnv no-gzip dont-vary

to your .htaccess file.

Can you confirm that the script I posted above works as you'd expect in your command line environment or not?

Also, if you are working within a web server, please post which web server you're using (IIS/apache/etc)

Upvotes: 1

Cobra_Fast
Cobra_Fast

Reputation: 16101

Forcing output to the browser works with the following calls in this order

ob_flush();
flush();

It, however, will not background any processing going on.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96266

You need threads or processes. This is a good thread: Does PHP have threading?

My favorite answer there is https://stackoverflow.com/a/14201579/650405

Upvotes: 1

Related Questions