duandu9260 2014-06-01 05:54
浏览 33
已采纳

错误的结果:两个变量的总和

I have a simple problem, but I can't solve it, still.

Working code

(int)$sum_price=4;
(int)$price_per_sum = (int)$spo[$key]*(int)$gpp['price'];
echo $sum_price = ($sum_price + $price_per_sum);

When I change 4 to a variable, the result of sum() is string.

Result becomes a string

 $spo[$key] = 1;
 $gpp['price'] = 2;
(int)$sum_price=4;
(int)$price_per_sum = (int)$spo[$key]*(int)$gpp['price'];
 echo $sum_price = ($sum_price + $price_per_sum);

The result is 42 but should be 6, instead. Using (int) does not change my result.

Problem code

# Get All Payment 
(int)$sum_price = 0;
$sum_price_product = explode('|',$_SESSION['product']);
$spo = explode('|',$_SESSION['order']);
foreach($sum_price_product as $key=>$spp)
{
  if($spp!='')
  {
    $get_product_price = $dbc->select("sh_product"," id = '{$spp}'","id");
    $gpp = mysql_fetch_array($get_product_price['sql']);
    (int)$price_per_sum = $spo[$key] * $gpp['price'];
    $sum_price = $sum_price + $price_per_sum;
  echo $sum_price;
  }
}

http://codepad.org/Zo9X2PY5

  • 写回答

2条回答 默认 最新

  • dsa2c2255888 2014-06-01 06:36
    关注

    Casting is not required in this case. Please review below code and try it out,its working for me:

    $spo[0] = 1;
    $gpp['price'] = 2;
    $sum_price=4;
    $price_per_sum = $spo[0]*$gpp['price'];
    $sum_price = $sum_price + $price_per_sum;
    echo $sum_price;
    

    Here in $spo[0], i have considered single value but you can replace '0' by $key in loop construct.

    Please review here my working code: http://codepad.org/zH2q2WjH

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料