douhong4452 2016-10-10 00: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 01: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 pycharm倒入虚拟环境的时候,显示这个,但是我的虚拟环境已经创建了
  • ¥15 FPGA芯片60进制计数器
  • ¥15 前端js怎么实现word的.doc后缀文件在线预览
  • ¥20 macmin m 4连接iPad
  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部