douci7521 2015-09-09 23:00
浏览 37
已采纳

PHP中一个总和的未定义变量[关闭]

I am making a shopping cart in php, its almost done and now i'm making the total of all items in the cart with:

while ($data=mysql_fetch_array($result)){

$total=$price+$total;

}

It returns "Undefined variable:total" but it works anyways and i got the correct result. Its very strange and i don't want this error showing up.

When i define the variable like in the example below it doesn't give me the right result, just gives me the last value of the '$price' variable and not the sum.

$total=0;
$total=$price+$total;

Can someone give me a hint, seems very simple to solve but i can't do it. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • 普通网友 2015-09-09 23:16
    关注

    move your

    $total=0; 
    

    above your while and the undefined error should be gone. The reasons is clear, you're trying to add $price to undefined variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等