Php 计算错误,请帮我查找?

Php 计算错误,请帮我查找?,php,debugging,Php,Debugging,我正在我的网站上运行订阅。 我有一个1、3、6和12个月的订阅,我希望用户能够随时更改订阅。 然而,我需要计算用户在短期(而不是相对便宜的长期)注册时必须支付的金额 我创建了这个函数优化了订阅总数($active\u sub\u time,$arr\u sub\u value),这样它就可以准确地返回那笔钱 <?php function optimized_subscription_total($active_sub_time,$arr_sub_values) { // T

我正在我的网站上运行订阅。 我有一个1、3、6和12个月的订阅,我希望用户能够随时更改订阅。 然而,我需要计算用户在短期(而不是相对便宜的长期)注册时必须支付的金额

我创建了这个函数
优化了订阅总数($active\u sub\u time,$arr\u sub\u value)
,这样它就可以准确地返回那笔钱

<?php 


function optimized_subscription_total($active_sub_time,$arr_sub_values)
{

    // This function takes a row from the DB where prices for each period of time usage is listed. there are prices for 1 month, 3 months,6 and 12 months.

    // when the user has subscribed for 12 months, and the user asks for a refund, after they used 9 months and 6 days for example, the system treats the refund as if they subscribed for (in months) COST_6 + COST_3 + (COST_1/30)*6 
    // the result of the function is then subtracted from the amount they actually paid and is considered the refund.

    // $arr_sub_values is the associative row from the DB, containing the prices
    // $active_sub_time is measured in months and is a double

$result=0;


while(($active_sub_time-12)>=0)
{
    $active_sub_time-=12;
    $result+=($arr_subscription_values['COST_12']);
}

while(($active_sub_time-6)>=0)
{
    $active_sub_time-=6;
    $result+=($arr_subscription_values['COST_6']);
}
while(($active_sub_time-3)>=0)
{
    $active_sub_time-=3;
    $result+=($arr_subscription_values['COST_3']);
}

while(($active_sub_time-1)>=0)
{
    $active_sub_time-=1;
    $result+=($arr_subscription_values['COST_1']);
}

if($active_sub_time>0)
    $result+=($active_sub_time)*($arr_subscription_values['COST_1']);

return $result;

}



$datetime1 = date_create('2009-12-11');
$datetime2 = date_create('2010-11-09');
$interval = date_diff($datetime1, $datetime2);
$num_of_months = ($interval->format('%y'))*12+($interval->format('%m'))+($interval->format('%a'))/30;
echo "<br />";

$v = array('COST_1'=>'3.99','COST_3'=>'9.99','COST_6'=>'15.99','COST_12'=>'25.99');
echo "OPT value for $num_of_months months=" . optimized_subscription_total($num_of_months, $v);


?>
因此,在这里。我想我需要一个浮点数,不是吗


我期望函数的结果应该是“10个月的OPT值=29.97”,因为它应该是COST_6+COST_3+COST_1。。。但是我得到了奇怪的M.97,有时像pokhhg.97之类的东西,我会将逻辑更改为以下内容,看看是否仍然产生错误。我认为这更清晰,更易于调试。这与你的想法相同,只是解释不同而已

while($active_sub_time>=12)
{
    $active_sub_time-=12;
    $result+=($arr_subscription_values['COST_12']);
}

while($active_sub_time>=6)
{
    $active_sub_time-=6;
    $result+=($arr_subscription_values['COST_6']);
}
while($active_sub_time>=3)
{
    $active_sub_time-=3;
    $result+=($arr_subscription_values['COST_3']);
}

while($active_sub_time>=1)
{
    $active_sub_time-=1;
    $result+=($arr_subscription_values['COST_1']);
}
我还要做的是在函数的顶部添加debug cod

echo "<br>$active_sub_time" // debug code include at the top
echo“
$active\u sub\u time”//debug代码包含在顶部
如果有垃圾被发送到函数本身,这将给您一个想法

此外,如果上述方法不能解决问题,我会在所有while块中添加此测试函数

if (!is_numeric($result))
{
    echo"<br> Bug occurred";break; // print other values if necessary
}
如果(!是数值($result))
{
echo“
出现错误”中断;//必要时打印其他值 }
他从什么变成了什么?例如,我的订阅期为12个月,我的订阅期为2个月,现在我将订阅期更改为3个月,您将所有这些值存储在何处?这只是有关此功能的一些概述。如果您全年(12个月)支付了25.99英镑,并在7个月后进行了纾困或升级,则您将被视为拥有6个月+1个月。并从之前已支付的25.99中获得退款。在这里你可以看到它是:return=25.99-(15.99+3.99)=6.01如果你使用这个函数并运行它,10次之后你可以看到输出是错误的。比如“10个月的选择值=M.97”,其中M.97很奇怪。我正在试图找出原因。我没有测试代码,因为我目前没有PHP服务器,如果事情仍然困扰着你,请告诉我。是的。仍然发生。。。这一定是内存泄漏。多亏了鳄鱼猎人,我刚刚在函数tail中添加了is_数字。这太奇怪了,我得到了打印出来的值(现在是I.00425),但服务器正常处理它,它只是这样打印出来。我得到的gettype是一个double。函数是否也为您返回了错误的值?
if (!is_numeric($result))
{
    echo"<br> Bug occurred";break; // print other values if necessary
}