如何删除“for”循环中的前一个变量并添加下一个变量?

我正在尝试制作一个刽子手游戏,其中“for”循环滚动不同的图像,但我无法让前一个图像被下一个图像替换,这最初是通过矢量绘制的,但我走了另一种方式,任何建议,下面是代码:


<?php

    $dude = [

        '<img id="hung"src="images/hangman00.png"/>',

        '<img id="hung"src="images/hangman01.png"/>',

        '<img id="hung"src="images/hangman02.png"/>',

        '<img id="hung"src="images/hangman03.png"/>',

        '<img id="hung"src="images/hangman04.png"/>',

        '<img id="hung"src="images/hangman05.png"/>',

    ];

    

    function drawdude(){

        global $dude;

    

    

    for ($i=0; $i <= $_SESSION['numwrong']; $i++) {

        echo $dude[$i];

    }

        echo '</img>';

    }

?>


翻过高山走不出你
浏览 88回答 1
1回答

largeQ

该函数不应使用for loop. 它将打印所有符合条件的条件。您需要 acondition来显示一张图像。这是代码<?php&nbsp; &nbsp; // Just for the sake of calling&nbsp; &nbsp; $_SESSION['numwrong'] = 2;&nbsp; &nbsp; $dude = [&nbsp; &nbsp; &nbsp; &nbsp; '<img id="hung"src="images/hangman00.png"/>',&nbsp; &nbsp; &nbsp; &nbsp; '<img id="hung"src="images/hangman01.png"/>',&nbsp; &nbsp; &nbsp; &nbsp; '<img id="hung"src="images/hangman02.png"/>',&nbsp; &nbsp; &nbsp; &nbsp; '<img id="hung"src="images/hangman03.png"/>',&nbsp; &nbsp; &nbsp; &nbsp; '<img id="hung"src="images/hangman04.png"/>',&nbsp; &nbsp; &nbsp; &nbsp; '<img id="hung"src="images/hangman05.png"/>',&nbsp; &nbsp; ];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function drawdude(){&nbsp; &nbsp; &nbsp; &nbsp; global $dude;&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_SESSION['numwrong']) && $_SESSION['numwrong'] <= 5){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $dude[$_SESSION['numwrong']];&nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</img>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; // Call the function&nbsp; &nbsp; drawdude();?>
打开App,查看更多内容
随时随地看视频慕课网APP