2

Please excuse what may seem like a very simple question and may lack of understanding of how PHP/sleep() works, but I was after some guidance on the following code:

<?php
$time_now = time();
echo "Time Now : " . $time_now;
sleep(10);
$time_then = time();
echo "<br>Time Then : " . $time_then;
?>

I was expecting this code to output the current time, then wait 10 seconds, then output the time 10 seconds later.

Instead it waits the 10 seconds set and outputs both the times at the same time. The times ARE correct in that the 'Time Now' is when I executed the code, and the 'Time Then' is 10 seconds later, but it does not respond how I expected (output Time Now, wait 10 seconds, then continue to execute the code and show 'Time Then').

Given that my understanding is clearly incorrect, is it possible to achieve this? Or would I be better to trigger another file to load what I want to do as follows:

File 1

<?php
$time_now = time();
echo "Time Now : " . $time_now;
header('Location: page2.php');
?>

File 2

<?php
sleep(10);
$time_then = time();
echo "<br>Time Then : " . $time_then;
?>

Thank you.

omega1
  • 1,031
  • 4
  • 21
  • 41
  • The `sleep()` command, is used for telling the server to wait before executing your script. It seems it doesn't matter if you put the function in the begin, middle or end, it sleeps anyway. – node_modules Mar 21 '16 at 08:49
  • @C0dekid Thank you, I understand that, but I expected the code before the sleep command to execute and echo before then waiting and echoing the second value – omega1 Mar 21 '16 at 08:50
  • Output is buffered in general. If you want an immediate response when calling `echo` you need to flush your output. – Phylogenesis Mar 21 '16 at 08:50
  • It does work the way you were expecting: just try it in the command-line! What you're probably not mentioning is that you're loading code through a web server and web browser combination. – Álvaro González Mar 21 '16 at 08:52
  • Take a look at this example: http://php.net/manual/en/function.flush.php I think this is what you need. PHP needs to be executed in one time. So basically the message before sleeping will not be show because the sleep() function is waiting to execute the rest of the script. – node_modules Mar 21 '16 at 08:52
  • @ÁlvaroGonzález You are correct, my apologies if I missed this information. – omega1 Mar 21 '16 at 08:55

2 Answers2

6

You need to use the flush function of PHP, to get the excepted result.

PHP gives you the output, when script has finished. If you want to show the result in real time, you need to flush the output buffer.

$time_now = time();
echo "Time Now : " . $time_now;
flush();
sleep(10);
$time_then = time();
echo "<br>Time Then : " . $time_then;
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • 1
    Thank you, but that gives me the same result – omega1 Mar 21 '16 at 08:52
  • Read the comments at http://php.net/manual/en/function.flush.php - Server and Browser may also buffer output. Try to send more text (whitespace and at least one newline) and check the web server configuration. – NineBerry Mar 21 '16 at 08:56
  • 1
    Please check this: http://stackoverflow.com/questions/4706525/php-flush-not-working – vaso123 Mar 21 '16 at 08:57
  • Understood, this works OK in CLI but not while loading via web browser. Thank you for your help! – omega1 Mar 21 '16 at 09:03
1

If you run it on server it will indeed give the output after the script finished.

But if used as a command line script it will output then sleep for ten seconds and then continue

In @lolka_bolka mentioned if you want value then and there you can use flush

techblu3
  • 170
  • 9