dongliuxia9495 2016-04-16 03:47
浏览 35
已采纳

php太多if else声明

I have php code as below:

if($ptp_car_1 == "ptp_Denpasar_Ubud_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
    }
    if($ptp_car_1 == "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
    }
    if($ptp_car_1 == "ptp_Denpasar_Sanur_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
    }
    if($ptp_car_1 == "ptp_Denpasar_Seminyak_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
    }
    if($ptp_car_1 == "ptp_Denpasar_Kuta_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
    }
    if($ptp_car_1 == "ptp_Denpasar_Jimbaran_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
    }
    if($ptp_car_1 == "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1"){
        $ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
    }
// many code over 1,000

How can i optimize it ? I want the page run faster, now is too slow for it.

  • 写回答

5条回答 默认 最新

  • douning5041 2016-04-16 04:08
    关注

    in this case, SWITCH CASE should be better.

    go through this...

    Why the switch statement and not if-else?

    Is "else if" faster than "switch() case"?

    give a try to this,

    switch ($ptp_car_1){   
            case "ptp_Denpasar_Ubud_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
            }
            break;
            case "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
            }
            break;
            case "ptp_Denpasar_Sanur_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
            }
            break;
            case "ptp_Denpasar_Seminyak_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
            }
            break;
            case "ptp_Denpasar_Kuta_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
            }
            break;
            case "ptp_Denpasar_Jimbaran_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
            }
            break;
            case "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1":
            {
                 $ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
            }
            break;
    
            /*
            //OPTIONAL
    
            default:
                $ptp_car_1 = $ptp_car_1;
    
            */   
    
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论