drlma06060 2015-11-19 10:39
浏览 11
已采纳

如何缩短这个设定值检查?

This is my first post. I hope I'm doing this right. I have written this little piece of code, it checks some request parameters. Has anyone a clue how to write this in a shorter and maybe better performance way?

$request = $_REQUEST;
if ( isset($request['customerId']) && 
    ! $request['customerId']  == '' && 
     isset($request['subscriptionId']) && 
     ! $request['subscriptionId']  == '' && 
     isset($request['customer_ext_uid']) && 
     ! $request['customer_ext_uid']  == '' && 
     isset($request['referrer']) && 
     ! $request['referrer']  == '' && 
     $request['referrer']  == 'fastbill' || 
     $request['referrer']  == 'paypal' && 
     $request['do']  == 'success' && 
     $request['do']  == 'success' ) :
    echo "good";
else :
    echo "bad";
endif;

In the end the success URL should look like this:

https://DOMAIN/?customerId=12347&customer_ext_uid=12347&subscriptionId=12347&referrer=paypal&do=success

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dsgawmla208057 2015-11-19 11:06
    关注

    One way to do it could be like this, it'll also make it easier to add more checks in the future, and / or reuse it.

    $required = array('customerID','subscriptionId'); //Fill in the fields you want checked
    $request = $_REQUEST;
    
    function allOk($required,$request){
        foreach($required as $key){
            if(empty($request[$key])){
                return FALSE;
                die();
            }
        }
    
        if($request['do'] != 'success' || ($request['referrer'] != 'fastbill' && $request['referrer'] != 'paypal')){
            return FALSE;
            die();
        }
    
        return TRUE;
    }
    

    And then you can either use a if sentence or a ternary operator, depending how much action you want according to the result.

    if(allOk($required,$request)){
       echo "good!";
    }else{
       echo "BAD!";
    }
    

    Ternary operator:

    echo (allOk($required,$request)) ? 'good' : 'bad' ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?