我怎样才能更有效地做到这一点?

Rob*_*Rob 3 php

switch ($_POST['stealmeth'])
{
    case "Plimus":
        if (!is_plimus_ref($_POST['stealrefid']))
        {
            $errorArr[] = "Reference ID doesn't match the payment method.";
        }
        break;
    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;
}
Run Code Online (Sandbox Code Playgroud)

如你所见,我只是一遍又一遍地做同样的事情.

是否有更清洁或更有效的方法来做到这一点?

Gum*_*mbo 11

您可以使用变量变量:

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.";
        }
}
Run Code Online (Sandbox Code Playgroud)

你应该添加一个默认情况.