dongnei3634 2014-03-24 14:57
浏览 222
已采纳

为什么抛出这个PHP警告(除以零)?

I was receiving a PHP warning for 'Division By Zero' due to one of the equations on my site:

$VidListAppRate = (($VLA['Likes'] / $VLA['views']) * 100;

After I realized I did not account for the fact that the variable $VLA['views'] could be zero, I changed this code to the following, thinking it would eradicate the warning:

$VidListAppRate = ($VLA['views'] === 0) ? 0 : ($VLA['Likes'] / $VLA['views']) * 100;

However, the warning still appeared (note: $VLA['views'] is an int) . I tried replacing 0 with a string:

$VidListAppRate = ($VLA['views'] === 0) ? 'N/A' : ($VLA['Likes'] / $VLA['views']) * 100;

but still I receive the warning. I know notices, warnings, and error messages in my php error_log are my friends, but how would I rewrite my code to appease this warning?

  • 写回答

1条回答 默认 最新

  • doo6568 2014-03-24 15:00
    关注

    An exact match is expected here:

    $VidListAppRate = ($VLA['views'] === 0)...
    

    Maybe you need:

    $VidListAppRate = ($VLA['views'] == 0)...
    

    Or:

    $VidListAppRate = ($VLA['views'] === '0')... 
    

    Or:

    $VidListAppRate = (!$VLA['views'])...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化