重构If / Else语句

时间:2014-10-02 15:27:23

标签: php

我试图找到一种更好的方法来编写这种逻辑。使用PHP类FDF,我从中检查复选框的值,并将图像添加到PDF中的坐标。

        if ($salutation[0] == "Dr.") {

            $pdf->Image('/inc/checked.png',31.4, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',43.5, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',56, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',70.5, 105.5,-300);

        }elseif ($salutation[0] == "Mr.") {

            $pdf->Image('/inc/checked.png',43.5, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',31.4, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',56, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',70.5, 105.5,-300);

        }elseif ($salutation[0] == "Mrs.") {

            $pdf->Image('/inc/checked.png',56, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',31.4, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',43.5, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',70.5, 105.5,-300);

        }elseif ($salutation[0] == "Ms.") {

            $pdf->Image('/inc/checked.png',70.5, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',31.4, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',43.5, 105.5,-300);
            $pdf->Image('/inc/unchecked.png',56, 105.5,-300);
        }

switch语句看起来几乎是一回事。对我来说问题是,无论如何,仍然需要添加其他图像。有更好的方法吗?

5 个答案:

答案 0 :(得分:2)

怎么样

$coords = array(
    'Dr.' => array(31.4, 105.5,-300),
    'Mr.' => array(43.5, 105.5,-300),
    'Mrs.' => array(56, 105.5,-300),
    'Ms.' => array(70.5, 105.5,-300)
);

foreach ($coords as $title => $coord) {
    if($salutation[0] == $title) {
        $pdf->Image('/inc/checked.png', $coord[0], $coord[1], $coord[2]);
    } else {
        $pdf->Image('/inc/unchecked.png', $coord[0], $coord[1], $coord[2]);
    }
}

答案 1 :(得分:2)

最简单的选择是使用三元运算符。不要使用花哨的数组,函数或循环来复杂化这个简单的问题。所以你可以用4行代码替换整个事情:

$pdf->Image('/inc/' . (($salutation[0] == 'Dr.') ? '' : 'un') . 'checked.png',31.4, 105.5,-300);
$pdf->Image('/inc/' . (($salutation[0] == 'Mr.') ? '' : 'un') . 'checked.png',43.5, 105.5,-300);
$pdf->Image('/inc/' . (($salutation[0] == 'Mrs.') ? '' : 'un') . 'checked.png',56, 105.5,-300);
$pdf->Image('/inc/' . (($salutation[0] == 'Ms.') ? '' : 'un') . 'checked.png',70.5, 105.5,-300);

欢迎你。 :)

答案 2 :(得分:0)

尝试开关

switch($salutation[0]){
    case 'Dr.':
        //do stuff
    break;

    case 'Mr.':
        //do stuff
    break;

    case 'Mrs.':
        //do stuff
    break;
}

答案 3 :(得分:0)

您可以使用一个带4个数字的简单函数来简化图像的创建并减少重复的代码:

function handleImages($a, $b, $c, $d) {
    $pdf->Image('/inc/checked.png', $a, 105.5, -300);
    $pdf->Image('/inc/unchecked.png', $b, 105.5, -300);
    $pdf->Image('/inc/unchecked.png', $c, 105.5, -300);
    $pdf->Image('/inc/unchecked.png', $d, 105.5, -300);
}

然后,您可以在简化的开关中调用您的功能:

switch($salutation[0]){
    case 'Dr.':
        handleImages(31.4, 43.5, 56, 70.5);
        break;
    case 'Mr.':
        handleImages(43.5, 31.4, 56, 70.5);
        break;
    case 'Mrs.':
        handleImages(56, 43.5, 31.4, 70.5);
        break;
    case 'Ms.':
        handleImages(70.5, 56, 43.5, 31.4);
        break;
}

答案 4 :(得分:0)

因为这是PHP - 它会让我们的工作变得简单!您可以使用关联数组。

确实有更好的方法来执行以下操作 - 但我喜欢以下代码的简单性:

$checkedPos = array ("Dr." => 31.4, "Mr." => 43.5, "Mrs." => 56, "Ms." => 70.5);
$unchecked1Pos = array ("Dr." => 43.5, "Mr." => 56, "Mrs." => 70.5, "Ms." => 31.4);
$unchecked2Pos = array ("Dr." => 56, "Mr." => 70.5, "Mrs." => 31.4, "Ms." => 43.5);
$unchecked3Pos = array ("Dr." => 70.5, "Mr." => 31.4, "Mrs." => 43.5, "Ms." => 56);

$pdf->Image('/inc/checked.png', $checkedPos[$salutation[0]], 105.5, -300);
$pdf->Image('/inc/unchecked.png', $unchecked1Pos[$salutation[0]], 105.5, -300);
$pdf->Image('/inc/unchecked.png', $unchecked2Pos[$salutation[0]], 105.5, -300);
$pdf->Image('/inc/unchecked.png', $unchecked3Pos[$salutation[0]], 105.5, -300);

希望有所帮助。