Log in

View Full Version : how does php sleep function works



vinpkl
05-21-2015, 02:16 PM
Hi


If i run the below script then both the dates are displayed after delay of 15 seconds.

But i think 1st date should get displayed instantly and 2nd should get display after 15 seconds.


But its not working like that ??





<?php
echo date('H:i:s');
sleep(15);
echo "<br>";
echo date('H:i:s');
?>



Vineet

coothead
05-21-2015, 03:05 PM
Hi there vinpkl,


your reasoning is slightly flawed. ;)

You make a call to your server, the php script is run and when
this is fully completed, the result is returned to your browser.


coothead

styxlawyer
05-21-2015, 03:12 PM
PHP's output stream is normally buffered and nothing is sent to the browser until the script terminates. If you want to see the result of the first "echo" statement immediately, you need to flush the buffer before the "sleep()" statement is executed.



<?php
echo date('H:i:s');
flush();
sleep(15);
echo "<br>";
echo date('H:i:s');
?>

vinpkl
05-21-2015, 04:30 PM
Hi Styx

flush() also didnt made any difference.

The page is displayed with both dates after 15 seconds delay.

first date is not displayed immediately.

vineet

vinpkl
05-21-2015, 04:32 PM
Hi there vinpkl,


your reasoning is slightly flawed. ;)

You make a call to your server, the php script is run and when
this is fully completed, the result is returned to your browser.


coothead

Thats what is happening. the page is displayed with both dates after 15 seconds.

Vineet

coothead
05-21-2015, 05:48 PM
One call to the server, one reply. :rolleyes: