Echo 'String' While Every Long Loop Iteration (Flush() Not Working)

Echo 'string' while every long loop iteration (flush() not working)

From PHP Manual:

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

echo "Hello!";
flush();
ob_flush();

for($i = 0; $i < 10; $i ++) {
echo $i;
//5-10 sec execution time
flush();
ob_flush();
}

-or- you can flush and turn off Buffering

<?php
//Flush (send) the output buffer and turn off output buffering
while (ob_get_level() > 0)
ob_end_flush();

echo "Hello!";

for($i = 0; $i < 10; $i ++) {
echo $i . "\r\n";
}

?>

How to flush output after each `echo` call?

Edit:

I was reading the comments on the manual page and came across a bug that states that ob_implicit_flush does not work and the following is a workaround for it:

ob_end_flush();

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_start();

If this does not work then what may even be happening is that the client does not receive the packet from the server until the server has built up enough characters to send what it considers a packet worth sending.


Old Answer:

You could use ob_implicit_flush which will tell output buffering to turn off buffering for a while:

ob_implicit_flush(true);

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_implicit_flush(false);

php echo the result while iterating through the loop

PHP buffers the output.

If you want to output stuff to the browser immediately you can use the flush() and ob_flush() functions:

for ($i = 0; $i <= 680; $i += 40) {
$url = 'http://www.yelp.com/biz/franchino-san-francisco?start=80';
$root = yelp($url);
var_dump($root);
flush();
ob_flush();
}

Server response during a loop (status update)

You could store the progress in the Session and use a second ajax call to track the progress:

$filecounter = 0;
foreach ($files as $file) {
// add $file to zip archive
$filecounter++;
if ($filecounter % 50 == 0) {
session_start();
$_SESSION['progress'] = $filecounter;
session_write_close();
}
}

You need session_write_close(); to make the Session var accessible to the second script.

PHP | MySQLi : How to echo statement while WHILE-LOOP is running

Taken from this

You can use output buffering like this:

ob_start();

echo('doing something...');

// send to browser
ob_flush();

// ... do long running stuff
echo('still going...');

ob_flush();

echo('done.');
ob_end_flush();

How to echo every after 9th iteration of a loop? First echo only counted 8 items

I was able to solve it by modifying the condition:

So instead of: if($i % 9 == 0) {...}

I used: if($i!=0 && $i % 9 == 0) {...}

And also placing $i++ at the end of while loop.

PHP: How to progressively output inside a loop?

Flushes the system write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.

<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++) {
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
ob_flush();
flush();
sleep(2);
}
echo "Done.";
ob_end_flush();
?>

Read documentation here.

EDIT:

str_pad() - Pad a string to a certain length with another string.
and Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page. that's why use str_pad() for be safe side.



Related Topics



Leave a reply



Submit