1

This may be a simple question but, I am not an seasoned PHP developer and I am hoping someone can show me some light.

Suppose I have PHP array:

$items = array('apple', 'banana', 'orange');

Nomarlly, when run the loop, using PHP foreach loop, it will go through each iteration one after another without any time delay.

What I need is,

foreach ($items as $item) {
Code to print 'apple'
Code to wait 5 seconds
Code to Print 'banana'
Code wait 5 seconds
.... And so on
}

It has to be strictly PHP no JavaScript please.

Thanks in advance.

sbedigital
  • 21
  • 4
  • 2
    Does this answer your question? [Php, wait 5 seconds before executing an action](https://stackoverflow.com/questions/6730855/php-wait-5-seconds-before-executing-an-action) – El_Vanja Apr 29 '21 at 09:16
  • Also see: [php output with sleep()](https://stackoverflow.com/questions/3445222/php-output-with-sleep). – showdev Apr 30 '21 at 01:26

1 Answers1

0
$items = ['apple', 'banana', 'orange'];
foreach ($items as $item) {
    print $item. PHP_EOL;
    sleep(5);
}
kovalenko-alex
  • 183
  • 1
  • 10
  • Sorry guys… I fell sick, I was unable to test it out … I will test this and then update you accordingly. Again sorry for the delayed response. – sbedigital May 26 '21 at 23:13
  • Ok … to an extent this worked, but instead of printing each items in the one by one, this simple waits about 15 seconds then prints all three elements of the array. Logical outcome I was looking for, print “Apple” then wait 5 seconds, print “banana” under the “Apple” and lastly, print “orange” under Banana and Apple. – sbedigital May 26 '21 at 23:19
  • @sbedigital that's... not how it works for me. how do you run the script? – kovalenko-alex May 28 '21 at 14:41