dougou5844 2019-07-04 23:10
浏览 50
已采纳

使用循环PHP 5.6时,中断,继续和退出问题

Switch statement works but, I am trying to break out of the loop but not having much luck.

I tried break, break 1, break 2 and continue but its not working. I am using PHP 5.6. Echo works fine and the data is coming out of the DB just fine. The problem is its printing three times.

    <?php 
    $sql = "SELECT * 
    FROM table1
    INNER JOIN verify
    WHERE uname='$showuser'";

    $result = mysqli_query($mysqli,$sql);

    if(mysqli_num_rows($result) > 0){  
        while($row = mysqli_fetch_array($result)){  
        $userid= $row["id"];
        $verify= $row["verified"];

    //NB! Notice how the If and While Loop are not closed here

    ?>

    <?php echo " stuff etc etc" ?>

        <?php 
            //I cant seem to some out of the IF statement. I tried BREAK, CONTINUE and EXIT.
            switch ($verify) {
            case '1': echo "<img src='passed.png' />"; break 1;
            case '3': echo "<img src='failed.png' />"; break 1;

            default:  echo "<img src='waiting.png' />"; break;
            }//END switch
        ?>


        <?php }} //END first IF & WHILE loops

        $sql2 = "SELECT * FROM table2 WHERE e.ctable_id = $userid ";

        $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error($mysqli));

        if(mysqli_num_rows($result2) > 0){  
            while($row2 = mysqli_fetch_array($result2)){  

        ?>

        <?php echo " stuff etc etc" ?>

        <?php } } //END second IF & WHILE loops ?>

Everything was fine before I added the switch statement.

  • 写回答

2条回答 默认 最新

  • dprxj1995 2019-07-04 23:19
    关注

    break ends execution of the current for, foreach, while, do-while or switch structure.

    break 1 will only break out of your "switch" statement, you will need break 2 to reach the while loop nesting level.

    $count = 0;
    while( $count++ < 10 )
    {
      echo 'while';
      switch( true )
      {
        case true:
          echo 'switch';
          break 2; // TWO is needed here to break out of TWO levels of nesting
      }
    }
    

    https://www.php.net/manual/en/control-structures.break.php

    break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.

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

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)