我得到了以下代码片段,它可以工作并完成我需要它做的事情。唯一的问题是我认为它有点“冗长”,可以做一些优化。如果可能的话,有人可以帮助我减少重复性。大部分代码都非常相似......
非常感谢
<?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 ;}
;} ?>