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;
}

正如你所看到的,我只是一遍又一遍地做同样的事情。

有没有更干净或更有效的方法来做到这一点?

最佳答案

您可以使用 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.";
        }
}

您可能应该添加一个默认情况。

关于php - 我怎样才能更有效地做到这一点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5613506/

10-13 05:48