douhu2890 2015-08-14 11:33
浏览 258

我们可以在switch语句中使用goto语句吗? 在PHP中

i m new to php and coding is old school

<?php
  for($i=-2; $i<count($s2)-1;$i++){ ?>
  <tr>
    <td>
    <?php
    switch ($i) {
       case '-2':       
        echo "JR KG";  
        goto a;        
        break;

        case '-1':
        echo "SR KG";
        break;

        case '0':
        echo "Nursery";
        break;

        default:
        echo $i;
    ?>
    </td>
    <?php foreach($a2 as $aaa){  a: ?>
    <td>
        <input type="checkbox" name="<?php echo $aaa.'['.$i.']'; ?>" >
    </td>
    <?php
       }
    ?>                              
</tr>
<?php
   }
}
?>

by above code i wanted to display list of standard and display a checkbox for each division (a-h). after printing standard i want to display the grid like checkboxs. this works for i= 1 to 12.. fails for -2,-1,0 because of break();

so i tried adding a goto a; but getting following error

Fatal error: 'goto' into loop or switch statement is disallowed in

any suggestions how to get the checkboxes for each division for the 1st three fields.

with the loop of "i" i am starting a row that display standard, and with the loop of "foreach($a2)" i am want to append number of checkboxes foreach divison in $a2.. want to create like a grid. its failing to append the checkboxes in first 3 rows because of break()

thanks.

  • 写回答

1条回答 默认 最新

  • doumaqing6652 2015-08-14 12:14
    关注

    I am still no totally sure I understand.

    But if you want to do something different in the three special cases try this general idea.

    <?php
    function add_checkboxes($i, $a2) {
        // do the checkbox processing in here
        if ( $i == -2 ) {
            foreach($a2 as $aaa){  a: ?>
               echo '<td>';
               echo  '<input type="checkbox" name="' . $aaa . '['.$i.'] .'" >';
               echo '</td>';
            }
        }
    }
    
    
    for($i=-2; $i<count($s2)-1;$i++){ 
    ?>
      <tr>
        <td>
    <?php
        switch ($i) {
            case '-2':       
                echo "JR KG";  
                add_checkboxes($i, $a2);
                break;
    
            case '-1':
                echo "SR KG";
                add_checkboxes($i, $a2);
                break;
    
            case '0':
            echo "Nursery";
                add_checkboxes($i, $a2);
                break;
    
            default:
                echo $i;
    ?>
        </td>
    </tr>
    <?php
       }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗