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条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿