这是我的情景:
如果单位是> 300然后充电Rs 7卢比/单位
if($units > 100){
$bill = 100 * 1;
$remaining_units= $units -100;
if($remaining_units > 100 ){
$remaining_units= $remaining_units -100;
$bill = $bill + (100* 2);
if($remaining_units > 100 || $remaining_units < 100){
$remaining_units= $remaining_units -100;
$bill = $bill + (100* 3);
if($remaining_units > 100 || $remaining_units < 100){
$bill = $bill + (100* 7);
}
}
}
}
echo $bill;
答案 0 :(得分:0)
使用此功能此功能适用于您:
echo calculate_bill(210);
function calculate_bill($units, $multipler = 1, $oldBill=0){
if($units > 100){
$remainingUnits = $units -100;
$oldBill = (100 * $multipler) + $oldBill;
}else{
$a = ($units * $multipler) + ($oldBill);
return $a;
}
/*first level complete*/
if($multipler == 1){
$multipler = 2;
}elseif($multipler == 2){
$multipler = 3;
}elseif($multipler >= 3){
$multipler = 7;
}
return calculate_bill($remainingUnits,$multipler, $oldBill);
}
答案 1 :(得分:0)
雅现在正常工作
if($units < 100){
$bill = $units*1;
}elseif($units > 100 && $units <=200){
$temp = 100*1;
$remaining_units = $units - 100;
$bill = $temp + ($remaining_units *2);
}elseif($units > 200 && $units <=300){
$temp = (100*1)+ (100*2);
$remaining_units = $units - 200;
$bill = $temp + ($remaining_units *3);
}else{
$temp = (100*1)+ (100*2)+ (100*3);
$remaining_units = $units - 300;
$bill = $temp + ($remaining_units *7);
}
echo $bill;