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.

2

1 Answer 1

0
$items = ['apple', 'banana', 'orange'];
foreach ($items as $item) {
    print $item. PHP_EOL;
    sleep(5);
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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 that's... not how it works for me. how do you run the script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.