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条)

报告相同问题?

悬赏问题

  • ¥50 HAL ADCDMA单次触发转换
  • ¥15 关于#python#的问题:我知道这个问题对你们来说肯定so easy
  • ¥15 wpf datagrid如何实现多层表头
  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置