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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比