为什么$ grand total显示不正确?

时间:2009-12-31 11:21:10

标签: php

让我们说$ _SESSION ['totalprice']是1200。 然而echo $ totalprice;输出1200和echo $ grandtotal;产出66. Grandtotal应为1265.

我在这里做错了什么?

$totalprice = $_SESSION['totalprice'];
$shipping= 65;

if (count($_SESSION['cart'])){
 $count = 1;
 foreach ($_SESSION['cart'] as $PID => $row){ 
  echo "<p class='padnmgn'><b>". $row['count'] . " " . $row['name'] . " @ " . $row['price']."</b></p><br/>\n";
  echo "<input type='hidden' name='item_name_".$count."' value='".$row['name']."'/>\n";
  echo "<input type='hidden' name='item_quantity_".$count."' value='".$row['count']."'/>\n";
  echo "<input type='hidden' name='item_price_".$count."' value='".$row['price']."'/>\n";
  echo "<input type='hidden' name='item_currency_".$count."' value='NOK'/>\n";
  echo "<input type='hidden' name='ship_method_name_".$count."' value='Posten'/>\n";
  echo "<input type='hidden' name='ship_method_price_".$count."' value='65.00'/>\n";

 }
}
$grandtotal = $totalprice + $shipping;

echo $totalprice;
echo $grandtotal;

2 个答案:

答案 0 :(得分:1)

试试这个:

$grandtotal = ((int) $totalprice) + $shipping;

答案 1 :(得分:0)

你确定$totalprice里面的值实际上是一个整数吗?你确定它没有被修改之后再进行添加吗?在添加之前尝试执行var_dump($totalprice);,看看它在那时真正具有什么价值。

如果它确实具有您期望的值,那么您可能需要明确地将其强制转换为整数才能使计算正常工作,因此类似(int)$totalprice + $shipping;等。

例如,

<?php
    $totalprice = "1200blahblah";
    $shipping = 65;
    $grandtotal = (int)$totalprice + $shipping;
    echo $grandtotal; // still prints "1265"
?>