douhong4452 2016-10-10 08:50
浏览 20
已采纳

虽然循环没有显示?

I am trying to write out all the array values using a while loop but it isn't working, any tips?

$console = array("Wii","Playstation 3","Xbox","Dator");
echo "I like " .$console[0] . " and " . $console[1] . ".";

while($console <5){
    echo "The console you like is: $console <br>";
    $x++;
}

My first echo is visible but the second one in my loop is not, what have I done wrong? The while loop should be able to show all 4 consoles variables but it doesn't show anything.

  • 写回答

3条回答 默认 最新

  • douhezhang8932 2016-10-10 09:03
    关注

    You can get your desired output by using foreach() and no need to use any extra function like count()

    Example:

    $console = array("Wii","Playstation 3","Xbox","Dator");
    foreach ($console as $key => $value) {
        echo $value."<br/>"; 
    }
    

    Result:

    Wii
    Playstation 3
    Xbox
    Dator
    

    Also note that, while($console <5){ this condition will make your loop to infinite, if you still want to use while() loop than you can check other answer or you can follow this example also.

    From the Manual:

    <?php
    $console = array("Wii","Playstation 3","Xbox","Dator");
    while (list ($key, $val) = each ($console) ) 
        echo $val."<br/>"; 
    ?>
    

    Result:

    Wii
    Playstation 3
    Xbox
    Dator
    

    You can also explore the manual: http://php.net/manual/en/control-structures.while.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题