Php output with sleep ()

I am trying to run a cycle every second for 25 seconds basically.

for($i = 0; $i <= 25; $i += 1){ 
    echo $i;
    sleep(1)
}

The fact is that it is not displayed until it is completely executed, therefore, after the cycle continues 25 times. Is there any way to do this, so that it appears before every dream? and not wait for the completion of the full cycle?

Thank!

+5
source share
3 answers

What you are trying to achieve is incremental output to the browser from PHP.

Whether this is achievable, your server may depend on how you call PHP.

PHP in FastCGI

, , PHP FastCGI, Apache, PHP . FastCGI , PHP, . , PHP, , , , - .

, ob_end_flush() ( ob_flush()) flush() - , PHP , . , , PHP-.

PHP mod_php

mod_php, . flush(), , PHP . - Apache, mod_gzip, . , PHP script , ( set_time_limit() PHP), , , , .

, , . IE 1 . , Chrome . , , 1 2 .

+8

flush PHP , .

for($i = 0; $i <= 25; $i += 1){ 
    echo $i;
    flush();
    sleep(1);
}

EDIT:

lighttpd , 4096 , , . GZIP flush. , , - HTTP.

, PHP proc . .

+4

bare-bones script, , .

<?PHP
ob_start();
$buffer = str_repeat(" ", 4096)."\r\n<span></span>\r\n";

for ($i=0; $i<25; $i++) {
  echo $buffer.$i;
  ob_flush();
  flush();
  sleep(1);
}

ob_end_flush();
?>

, , ( \r\n) ( ob_flush()). , .

+3

All Articles