PHP - errors with sleep ()
I am having some problems with the sleep () function in PHP.
<?php
echo date('h:i:s') . "<br>";
//sleep for 5 seconds
if(1 == 1){
sleep(5);
//start again
echo date('h:i:s');
}
?>
When I run this code, I get 5 seconds of pause, and then both dates inserted together, instead of one date, 5 seconds of pause, and then the next date.
Are there any alternative ways that I could write this, so it works correctly?
// turn off all layers of output buffering, if any
while (ob_get_level()) {
ob_end_flush();
}
// some browsers tend to buffer the first N bytes of output, refusing to render until then
// give them what they want...
echo str_repeat(' ', 1024);
echo date('h:i:s') . "<br>";
// force php to flush its output buffers. this also TRIES to tell the webserver to flush, but may not work.
flush();
sleep(5);
echo date('h:i:s');
flush();
, (). , , , , , , . .
! ob_start sleep a flush
1
ob_start();
echo date('h:i:s') . "<br>";
//sleep for 5 seconds
if(1 == 1){
sleep(5);
flush();
ob_flush();
//start again
echo date('h:i:s');
}
2
ob_implicit_flush(true);
echo date('h:i:s') . "<br>";
//sleep for 5 seconds
if(1 == 1){
sleep(5);
//start again
echo date('h:i:s');
}