dongnao2048 2016-07-25 22:50
浏览 15
已采纳

PHP虽然条件

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.

  • 写回答

1条回答 默认 最新

  • dongmu9253 2016-07-25 22:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了