"; //sleep for 5 seco...">

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?

+4
source share
2 answers
// 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();

, (). , , , , , , . .

+4

! 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');
}
0

Source: https://habr.com/ru/post/1534073/


All Articles