从1300开始500,并做一些与它
可以说我们有1300个单位。现在我想首先500个单位是12欧元的价格,另外800个是9欧元的价格。从1300开始500,并做一些与它
所以我基本需要的是一个函数,计算总数,并获得第一个500 * 12欧元和其他800 * 9欧元。但是总单位也可以是5000而不是1300单位的这个例子,它的用户输入为坐标。
<?php $total = 1300; 
$sub = 1; 
$sub2 = 1; 
for($i=1; $i<=$total; $i++) 
{ 
    for($sub; $i<=500; $sub++) 
    { 
     $sub2 += $sub; 
     break;  
    }  
} 
echo $sub2; 
?> 
回答:
<?php $units = 1300; 
$cut_off=500;//made it a var just in case 
$a=12;//fist 500 
$b=9;//rest 
//verbose calculations, you don't really need to create the extra variables 
$a_total=$cut_off*$a; 
$b_total=($units-$cut_off)*$b; 
$grand_total=$a_total+$b_total; 
?> 
很可能需要添加一个检查,如果估算的单位不到500或事情会变成怪异
回答:
更新时间:
<?php     function calc_total($qtt, $min = 500, $fst_price = 12, $nxt_price = 9){ 
     if($qtt > $min) return false; // Check if the minimum 500 was sent. 
     /* First calculate the rest, which is the quantity sent by 
     the user without the minimum x 9. Then add the minimum x 12 */ 
     $total = (($qtt - $min) * 9) + ($min * 12); 
     return $total; 
    } 
?> 
你可以调用的功能等这个:
echo "$". calc_total(1300); 以上是 从1300开始500,并做一些与它 的全部内容, 来源链接: utcz.com/qa/258073.html



