0

I'm trying to learn PHP and I wrote this code:

<?php

$number = 1;

while ($number < 21)
{
    echo "<p>I'm  " . "$number" . " years old!</p>";
    $number++;
}

?>

So my question is; is it possible to set-up something to see the sentences appear after a couple of seconds? Like this, first you see: I'm 2 years old!, then after like 3 seconds: I'm 3 years old! appears, then after another 3 seconds: I'm 4 years old! appears. Is that possible? Btw this is my first post, if I made a mistake please give feedback, it would help me in the future :)

Thank you

EDIT for possible future visitors: PHP isn't designed for this, but javaScript is.

  • just use javascript for that – Kevin Jul 25 '16 at 22:54
  • 3
    PHP isn't designed for what you want. Client-side JavaScript is much better performant. If you really want to do this in PHP, you can use a combination of `sleep` and `ob_flush` – Drakes Jul 25 '16 at 22:54
  • 1
    @Drakes `ob_flush` may not be enough, you will probably also need `flush` *and* to ensure there is no caching or GZIP compression through the server. Basically, just use JavaScript. – Alexander O'Mara Jul 25 '16 at 22:58
  • 1
    i would not call, pointing out the flaws in your approach, nitpicking. Its important information if the OP wants this to work –  Jul 25 '16 at 23:01
  • @AlexanderO'Mara Disable gzip, flush, bust cache, it's all a rich tapestry of hacking to get this to work in PHP. And my *approach* is JavaScript as I said. – Drakes Jul 25 '16 at 23:03
  • @Drakes The *"Basically, just use JavaScript."* part was for the benefit of the OP, so they don't mistake *possible* for *reasonable*. I was pointing out some more reasons they *really* should just use JavaScript. – Alexander O'Mara Jul 25 '16 at 23:05
  • Thank to all for the answers. After I learned PHP a bit and that I'm comfortable with it, I'll take a look at javaScript. Thanks again for the answers, good day! – Lorenzo Catalano Jul 25 '16 at 23:06
  • Possible duplicate of [php output with sleep()](http://stackoverflow.com/questions/3445222/php-output-with-sleep) – Alexander O'Mara Jul 25 '16 at 23:08

1 Answers1

1

PHP is a server side scripting language, and the code executes on server side. The final produced text is sent to the client as is and shown in the browser. If you want to animate the text or delay a couple of seconds, this has to be done in the client side, using Javascript (and NOT PHP).

The javascript for something like that would look like that:

var n = 1;
a = setInterval(function() {
    document.write("I'm " + n + " years old!");
    if (n++ >= 21) {
        clearInterval(a);
    }
}, 1000);

The client side would execute Javascript from what PHP sends, so if we send some Javascript code inside a <script> tag from PHP, it will be executed in the client side. So, we can do:

<?php
    echo "<script type='text/javascript'>var n = 1; a = setInterval(function() { document.write('I\'m ' + n + ' years old!'); if (n++ >= 21) clearInterval(a); }, 1000);</script>";
?>

Of course you don't really have to use PHP for that (you can just include the Javascript code inside a <script> tag and outside of <?php ?> tags, and it will be evaluated in the client side and not on the server side.

Yotam Salmon
  • 2,400
  • 22
  • 36
  • `document.write` will rewrite the whole document when called after it has closed. Also, echoing HTML is silly, just do `?> – Alexander O'Mara Jul 25 '16 at 22:59
  • @AlexanderO'Mara So? The document is empty, so nothing will be really removed. Edit: It is silly, but the question is tagged with PHP, so I felt obligated to do something with PHP :P – Yotam Salmon Jul 25 '16 at 23:00
  • @YotamSalmon Thanks for the answer. After I learned PHP a bit and that I'm comfortable with it, I'll take a look at javaScript. Thanks again for the answer, good day! – Lorenzo Catalano Jul 25 '16 at 23:08
  • @LorenzoCatalano More than welcome! And remember: PHP alone is garbage, Javascript alone is 4th grade kids' game. Only combined they have power. – Yotam Salmon Jul 25 '16 at 23:55
  • @LorenzoCatalano If the answer helped you, mark it as correct using the V sign below the vote count. This will mark the question as solved. – Yotam Salmon Jul 26 '16 at 00:28
  • @YotamSalmon Done! Thank you again for the help and the tip, good day! – Lorenzo Catalano Jul 26 '16 at 22:12