英文:
Division By Zero Error In Laravel Web App
问题
I am not a programmer, and I'm trying to fix an error I'm getting in a web app I had developed recently.
It's basically a project management app, and we are importing keyword rankings from ahrefs. Then we run some calculations to compare with previous months and years.
In researching the division by zero error, I understand this is more or less a math problem and we are trying to divide zero by zero or something. But it's all Chinese to me. So I'm hoping someone would be kind enough to spare a few minutes and help me fix whatever syntax in wrong in the function below?
This is from the app/helpers.php file inside my Laravel app.
If it helps, when I have debug on, it tells me this specific line is causing the error:
$percentage = $difference/$previous_month*100;
Thanks in advance, and the original full code for the function is below:
function getKeywordData($website)
{
if($website->lastMonthTop10Keywords)
{
$last_month = $website->lastMonthTop10Keywords->top3 + $website->lastMonthTop10Keywords->top4_10;
$previous_month = $website->previousMonthTop10Keywords->top3 + $website->previousMonthTop10Keywords->top4_10;
}
else
{
$last_month = 0;
$previous_month = 0;
}
$rankingData['last_month'] = $last_month;
$rankingData['value'] = '0';
$rankingData['arrow'] = 'down';
$rankingData['bootstrap'] = 'danger';
if($last_month >= $previous_month) {
$rankingData['arrow'] = "up";
$rankingData['bootstrap'] ="success";
$difference = $last_month - $previous_month;
$percentage = 0;
if($difference > 0)
{
$percentage = $difference/$previous_month*100;
}
}
else
{
$difference = $last_month - $previous_month;
$percentage = $difference/$previous_month*100;
}
$rankingData['value'] = round($percentage, 2);
return $rankingData;
}
Unfortunately, I am not a programmer, so I haven't tried any of the solutions already provided in similar questions about division by zero errors. It's not lack of effort or that I'm lazy. I'm just not versed enough in coding to be able to apply a fix unless the code is verbatim to my function, and I can simply cut and paste back into the file. I only understand CSS code.
英文:
I am not a programmer, and I'm trying to fix an error I'm getting in a web app I had developed recently.
It's basically a project management app, and we are importing keyword rankings from ahrefs. Then we run some calculations to compare with previous months and years.
In researching the division by zero error, I understand this is more less a math problem and we are trying to divide zero by zero or something. But it's all chinese to me. So I'm hoping someone would be kind enough to spare a few minutes and help me fix whatever syntax in wrong in the function below?
This is from the app/helpers.php file inside my laravel app.
If it helps, when I have debug on, it tells me this specific line is causing the error:
$percentage = $difference/$previous_month*100;
Thanks in advance and orig full code for function is below
function getKeywordData($website)
{
if($website->lastMonthTop10Keywords)
{
$last_month = $website->lastMonthTop10Keywords->top3 + $website->lastMonthTop10Keywords->top4_10;
$previous_month = $website->previousMonthTop10Keywords->top3 + $website->previousMonthTop10Keywords->top4_10;
}
else
{
$last_month = 0;
$previous_month = 0;
}
$rankingData['last_month'] = $last_month;
$rankingData['value'] = '0';
$rankingData['arrow'] = 'down';
$rankingData['bootstrap'] = 'danger';
if($last_month >= $previous_month) {
$rankingData['arrow'] = "up";
$rankingData['bootstrap'] ="success";
$difference = $last_month - $previous_month;
$percentage = 0;
if($difference > 0)
{
$percentage = $difference/$previous_month*100;
}
}
else
{
$difference = $last_month - $previous_month;
$percentage = $difference/$previous_month*100;
}
$rankingData['value'] = round($percentage, 2);
return $rankingData;
}
Unfortunately I am not a programmer, so I haven't tried any of the solutions already provided in similar questions about division by zero errors. It's not lack of effort or that I'm lazy. I'm just not versed enough in coding to be able to apply a fix unless the code is verbatim to my function, and I can simply cut n paste back into the file. I only understand css code.
答案1
得分: 1
$percentage = $previous_month === 0 ? 0 : $difference / $previous_month * 100;
在PHP中,这是一个条件判断语句,如果"previous month"的值为0,结果将为0,否则将执行除法计算。
英文:
$percentage = $difference / $previous_month * 100;
I assume it may occur that "there is no previous month", in which case we refer to it as "the first month". In this case, the value of the "previous month" is 0, and division by 0 is not possible. Therefore, we need to check whether the value of the "previous month" is 0 or not. If it is 0, we do not perform the division.If it is not 0, then we proceed with the division.
$percentage = $previous_month === 0 ? 0 : $difference / $previous_month * 100;
In PHP, there is an option to write a short if-else syntax as follows: "condition - true or false result" ? "what happens if true" : "what happens if false"
. In my case, I set the condition to check if the value of the previous month is 0. After the "?", I wrote 0 fixedly because I assume that in this case, the percentage value will be 0%. However, you can modify this value to anything you desire. After the ":", I included the original formula, as in that case, the value of the previous month is definitely not 0.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论