开发者

How can I do this more efficiently?

switch ($_POST['stealmeth'])
{
    case "Plimus":
        if (!is_plimus_ref($_POST['stealrefid']))
        {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }
        break开发者_C百科;
    case "LR":
        if (!is_lr_ref($_POST['stealrefid']))
        {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }
        break;
    case "PP":
        if (!is_pp_ref($_POST['stealrefid']))
        {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }
        break;
    case "AP":
        if (!is_ap_ref($_POST['stealrefid']))
        {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }
        break;
}

As you can see, I'm just doing the same thing over and over.

Is there a cleaner or more efficient way to do this?


You can use variable variables:

switch ($_POST['stealmeth']) {
    case "Plimus":
    case "LR":
    case "PP":
    case "AP":
        $f = 'is_'.strtolower($_POST['stealmeth']).'_ref';
        if (!$f($_POST['stealrefid'])) {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }
}

And you should probably add a default case.


Well, the problem is not your switch case, but the functions... you will have to rewrite the is_xx_ref function... Better post that code here, it probably can be rewritten to a general function, like this:

if (!is_ref( $_POST['stealmeth'], $_POST['stealrefid'] ) ){ $errorArr[] = "Reference ID doesn't match the payment method."; }


You can combine all the cases into one rule.

switch ($_POST['stealmeth'])
{
     case "Plimus":
     case "LR":
     case "PP":
     case "AP":
           if (!is_ap_ref($_POST['stealrefid']))
           {
                 $errorArr[] = "Reference ID doesn't match the payment method.";
            }
            break;
}


     $match = "";
     switch ($_POST['stealmeth'])
        {
            case "Plimus":
                $match=is_plimus_ref($_POST['stealrefid']);
                break;
            case "LR":
                $match=is_lr_ref($_POST['stealrefid']);
                break;
            case "PP":
                $match=is_pp_ref($_POST['stealrefid']);
                break;
            case "AP":
                $match=is_ap_ref($_POST['stealrefid']);
                break;
        }

        if(!$match) 
        {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }


$check_functions= array("Plimus"    =>  is_plimus_ref,
            "LR"        =>  is_lr_ref
            ...
            );


$check_function= $check_functions[$_POST['stealmeth']];         

if($check_function!=null && $check_function($_POST['stealrefid'])) 
{
    $errorArr[] = "Reference ID doesn't match the payment method.";
}


Surely you could use an if statement if it's just those four values?

if($_POST['stealmeth'] == "Plimus" || ... || $_POST['stealmeth'] == "AP"){
  if (!is_plimus_ref($_POST['stealrefid']))
    {
      $errorArr[] = "Reference ID doesn't match the payment method.";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜