dpc46827 2010-04-24 10:29
浏览 236
已采纳

在使用switch case循环时,我可以使用for循环来迭代这些情况吗?

In PHP, While using a switch case loop, can i use the for loop for iterating the cases? for example

switch .....

foreach($xyz as $abc)

{
 CASE:$abc
}

default;

UPDATE

I am fetching the value from DB, this value is name of table, by using "case" I want to execute a particular query according to the table name..

Is this possible?

  • 写回答

3条回答 默认 最新

  • douchong4730 2010-04-24 10:43
    关注

    You probably just want to put the switch statement into the foreach?

    foreach ($tables as $table) {
        switch ($table) {
            case 'table_one' :
                // do something here
                break;
            case 'table_two' :
                // do something here
                break;
            case 'table_three' :
                // do something here
                break;
            default :
                // do some error handling here
                break;
        }
    }
    

    Alternatively, a switch isn't that easy to read, consider going away from a switch and using an array-powered if, especially if you could dynamically create what you want to do each case:

    $tables = array('table_one', 'table_two', 'table_three');
    if (in_array($table, $tables)) {
        // do something here
    } else {
        // do some error handling here
    }
    

    That's a lot more readable, even if your array has a lot of elements.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?