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

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源