-2

I want something like:

for($k=0;$k<20;$k++){
    echo $k;
}

Output:

0

Sleep for 1 second.

1

sleep for 1 second.

2

sleep for 1 second.

.

. .

Federkun
  • 36,084
  • 8
  • 78
  • 90
Mohit Tiwari
  • 165
  • 2
  • 6
  • 1
    [`sleep`](http://php.net/manual/en/function.sleep.php) – Federkun Dec 23 '16 at 13:59
  • A topic to start with ^. – u_mulder Dec 23 '16 at 14:01
  • print where? cli? browser? Also why do you want this? – PeeHaa Dec 23 '16 at 14:01
  • 1
    PHP won't output anything (to the browser) until the script is finished executing, so if you do like the answers below suggests, you'll have to wait until it's all executed before anything outputs. You might want to use JS instead. – Qirel Dec 23 '16 at 14:01
  • if don't provide more detail, we can only give you [technically correct](https://www.youtube.com/watch?v=hou0lU8WMgo) answer, but not what you _really_ want. – Federkun Dec 23 '16 at 14:04

5 Answers5

2

If you're interested in having the variables appear on the page one by one, with a second delay in between, you'll need to use JavaScript rather than PHP. If you need input from PHP, use Ajax.

PHP has an inbuilt function called sleep() (http://php.net/manual/en/function.sleep.php) that causes the code to delay for a given number of seconds. However, this will not have the behaviour you might expect, which is to say, echoing a variable, then waiting a second, then echoing another one. The script will simply take that many seconds longer to execute.

So, for example:

for($k=0;$k<20;$k++){
    echo $k;
    sleep(1);
}

Will take 20 seconds to execute, but the page will still only load once.

CGriffin
  • 1,406
  • 15
  • 35
  • seems like theres a ninja downvoting everybody. Downvoter: explain why you downvoted – CodeGodie Dec 23 '16 at 14:04
  • Actually sleep function print 1 to 20 no together after execution of 20 seconds. – Mohit Tiwari Dec 23 '16 at 14:05
  • 1
    This way, he would still get the same result, but after a longer time, since the sleep is executed on the server and after the execution, the whole result is sent to the browser. You simply can't do this in PHP, you need to implement the client side. Edit: I removed my downvote after the answer edit, since it explains this behaviour now. – Lukas Körfer Dec 23 '16 at 14:06
  • 1
    lukegv and Mohit are 100% correct. I misunderstood the asker's intentions; I thought he just needed an artificial delay between echoing, not that he was hoping for a one-by-one echoing of a second in between each. Answer has been updated to reflect that. – CGriffin Dec 23 '16 at 14:09
2

I think you're looking for output buffer:

for ($k = 0; $k < 20; $k++) {
    echo $k . '<br />';
    flush();
    ob_flush();
    sleep(1);
}
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

If you intend to do this at CLI, then you need to use sleep, like this

for($k=0;$k<20;$k++){
    echo $k.'<br>Sleep for 1 second.';
    sleep(1);
}

However, if you intend to do this at the web-browser, then PHP runs at the server before the server responds to the client, therefore all your 20 seconds are spent on the server and the browser will receive all the output at once. Therefore, for browsers your approach is not adequate, instead of that, you might want to do a polling, or a setTimeout, or a setInterval. Since you are interested in PHP loops, I would like to suggest that you could run your PHP from command-line.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
-1

use inbuilt sleep function //time in seconds

for($k=0;$k<20;$k++){
    echo $k;
    sleep(1);
}
M A SIDDIQUI
  • 2,028
  • 1
  • 19
  • 24
  • 1
    Downvoter: explain why you downvoted – CodeGodie Dec 23 '16 at 14:04
  • 1
    This way, he would still get the same result, but after a longer time, since the sleep is executed on the server and after the execution, the result is sent to the browser. You simply can't do this in PHP, you need to implement the client side. – Lukas Körfer Dec 23 '16 at 14:06
  • I am not arguing and yes to show on browser ... php won't do that but in cli it will – M A SIDDIQUI Dec 23 '16 at 14:07
  • @lukegv depends on the context. If this is tried in the browser, then you are right. But since this works in CLI, may I suggest that we should ask the answerer to include that information into the answer instead of down-voting a correct-ish answer? – Lajos Arpad Dec 23 '16 at 20:40
-1

From http://docs.php.net/Thread with pthreads

<?php

class workerThread extends Thread {
public function __construct($i){
  $this->i=$i;
}

public function run(){
  while(true){
   echo $this->i;
   sleep(1);
  }
}
}

for($i=0;$i<50;$i++){
$workers[$i]=new workerThread($i);
$workers[$i]->start();
}
yakupars
  • 26
  • 4