重构如果/其他语句
我试图找到更好的方式来编写这个逻辑。使用PHP类FDF,我检查from的复选框的值,并将图像添加到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语句看起来好像差不多一样。对我来说问题是,无论如何,其他图像仍然需要添加。有一个更好的方法吗?
所有的最简单的方法是如何使用三元的运营商。不要通过使用花哨的数组,函数或循环来简化这个简单的问题。所以你可以用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);
不客气。 :)
同意。我喜欢一个很好的循环,但很难打败它。 – 2014-10-02 16:00:42
确实。我也喜欢好的圈,但我更喜欢三元运营商。 :) – 2014-10-02 16:11:08
尝试开关
switch($salutation[0]){
case 'Dr.':
//do stuff
break;
case 'Mr.':
//do stuff
break;
case 'Mrs.':
//do stuff
break;
}
我仍然需要每个案例添加4个图像。 – 2014-10-02 15:36:54
约
$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]);
}
}
数组中的第一行缺少逗号。 – 2014-10-02 16:29:01
@WonderBred好点 - 我已经纠正它。 – Jez 2014-10-03 10:18:06
您可以简化您的图像的创造和使用一个简单的函数,它的4个数字减少反复代码:
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;
}
:
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);
}
然后,您可以简化交换机中调用函数
由于这是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);
希望有所帮助。
'switch'语句适合这个面糊 – 2014-10-02 15:27:58
为什么不使用循环? – 2014-10-02 15:29:21