我得到了以下代码片段,它可以正常工作,并完成我需要它做的事情。唯一的问题是我认为它有点‘冗长’,可以做一些优化。如果可能的话,有人能帮我减少重复性吗?大部分代码都非常相似...
非常感谢
<?php
// condition = Less Than; More Than; Between
// noofweeks = this is the number of weeks chosen by the user
function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
if ($condition== "Between") {
// I need to get the start and end values as the data in this parameter should look like 1-17
$betweenArray = explode('-',$conditionWeeks);
$startWeek = $betweenArray[0];
$endWeek = $betweenArray[1];
}
if(($condition == "Less Than") && ($noofweeks < $conditionWeeks) && ($supplementType == 'Subtract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) ) { return $basicprice - $supplementAmnt; }
elseif(($condition == "Less Than") && ($noofweeks < $conditionWeeks) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) ) { return $basicprice + $supplementAmnt; }
elseif(($condition == "More Than") && ($noofweeks > $conditionWeeks) && ($supplementType == 'Subtract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) ) { return $basicprice - $supplementAmnt; }
elseif(($condition == "More Than") && ($noofweeks > $conditionWeeks) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) ) { return $basicprice + $supplementAmnt; }
elseif(($condition == "Between") && ($noofweeks >= $startWeek && $noofweeks <= $endWeek) && ($supplementType == 'Add') && ($weekno >= $weekStartno && $weekno <= $weekEndno) ) { return $basicprice + $supplementAmnt; }
elseif(($condition == "Between") && ($noofweeks >= $startWeek && $noofweeks <= $endWeek) && ($supplementType == 'Substract') && ($weekno >= $weekStartno && $weekno <= $weekEndno) ) { return $basicprice - $supplementAmnt; }
//if no conditions match, just return the unaltered basic price back
else { return $basicprice ;}
;} ?>发布于 2012-12-03 08:44:38
一个if列表可以写成嵌套的if。
最重要的变化:
($weekno >= $weekStartno && $weekno <= $weekEndno)是所有条件的一部分,所以它在外部if中。||),则将与给定条件字符串匹配的三个可变条件放入1。Add会导致加法运算。subtraction.中的Subtract
所有这些都归结为尽可能少地重复条件,尽管您应该记住结构仍然应该是清晰的。在某些情况下,这可能是更容易的方式。
<?php
// condition = Less Than; More Than; Between
// noofweeks = this is the number of weeks chosen by the user
function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
if ($condition == "Between") {
// I need to get the start and end values as the data in this parameter should look like 1-17
$betweenArray = explode('-',$conditionWeeks);
$startWeek = $betweenArray[0];
$endWeek = $betweenArray[1];
}
if ($weekno >= $weekStartno && $weekno <= $weekEndno)
{
if (($condition == 'Less Than' && $noofweeks < $conditionWeeks) ||
($condition == 'More Than' && $noofweeks > $conditionWeeks) ||
($condition == 'Between' && $noofweeks >= $startWeek && $noofweeks <= $endWeek))
{
// You can use a 'switch' as well, instead of if...elseif.
if ($supplementType == 'Subtract') {
return $basicprice - $supplementAmnt;
} elseif ($supplementType == 'Add' {
return $basicprice + $supplementAmnt;
}
}
}
return $basicprice;
} ?>在不同的设置中,我将中间结果放在单独的变量中,我认为这使它更具可读性。我在这里添加了一些注释,解释了这些决定:
function fmsSupplements($condition, $conditionWeeks, $noofweeks, $weekno, $weekStartno, $weekEndno, $basicprice, $supplementAmnt, $supplementType) {
// Create two 'helper' booleans to make the if condition easier.
$weekInRange = ($weekno >= $weekStartno && $weekno <= $weekEndno);
$noOfWeeksInRange =
($condition == 'Less Than' && $noofweeks < $conditionWeeks) ||
($condition == 'More Than' && $noofweeks > $conditionWeeks);
// Alternatively, you can break the single line above up in multiple
// lines, which makes debugging easier:
//$noOfWeeksInRange = ($condition == 'Less Than' && $noofweeks < $conditionWeeks);
//$noOfWeeksInRange |= ($condition == 'More Than' && $noofweeks > $conditionWeeks);
if ($condition == "Between") {
// I need to get the start and end values as the data in this parameter should look like 1-17
$betweenArray = explode('-',$conditionWeeks);
$startWeek = $betweenArray[0];
$endWeek = $betweenArray[1];
// Overwrite this variable with the condition that goes with 'Between'.
// We're already in that if, so we don't need to check 'Condition' again..
// You could use betweenArray[0] and [1] in the condition below, but using
// the variables $startWeek and $endWeek does make it more readable.
$noOfWeeksInRange = ($noofweeks >= $startWeek && $noofweeks <= $endWeek);
}
// And not this 'if' is even more readable.
if ($weeksInRange && $noOfWeeksInRange)
{
// You can use a 'switch' as well, instead of if...elseif.
if ($supplementType == 'Subtract') {
return $basicprice - $supplementAmnt;
} elseif ($supplementType == 'Add' {
return $basicprice + $supplementAmnt;
}
}
return $basicprice;
} ?>https://stackoverflow.com/questions/13675532
复制相似问题