php脚本错误gving错误的输出

时间:2011-03-28 09:27:36

标签: php

我在测试中总共20%的时候遇到一点麻烦我选择它所以'篮子'的值是60,我希望它显示60(它确实如此)但它应该工作超出60 + 20%这应该给读数72但是它给了144我想知道你是否偷看可以帮助

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Feet First</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <!-- The container holds the entire website everything else is 'contained' within the container -->
    <div class="container">
      <div class="header">

        <img src="header.png" alt="logo" width="892" height="142" id="logo" />
    </div>
      <!-- The content divis the Secondary layer which contains the content of the webiste such as text and images.  -->
      <div class="content"> 
        <!-- Navi is what hols the navigation links at the top of the page -->
       <div class="navi">
       <!-- this is the unordered list for the menu items -->
       <ul id="menu">
       <li><a href="\index.html">Home</a></li>
       <li><a href="\trainers.html">Trainers</a></li>
       <li><a href="\order.html" >Order</a></li>
       <li><a href="\credits.html">Credits</a></li>
       <li></li>
       <li></li>
    </ul>
    </div>

    <div class="content"></div>



        <h1>Thank you for your order!</h1>
    <?php

    // sents the value of the items
      $extraValues = array(
          'Laces' => 5,
          'Shoe Polish' => 10,
          'In-souls' => 15
       );

       $trainerValues = array(
          'Lacoste' => 50,
          'K-Swiss' => 45,
          'Puma' => 59,
          'Converse' => 65
       );

       if(isset($_POST['firstname'])){
          $firstname = $_POST['firstname'];
          echo " $firstname ";
       }

       if(isset($_POST['lastname'])){
        $lastname = $_POST['lastname'];
        echo "$lastname <br />\n";
       }

      if(isset($_POST['add1'])){
        $add1 = $_POST['add1'];
        echo "$add1 <br />\n";
      }

     if(isset($_POST['add2'])){
      $add2 = $_POST['add2'];
      echo "$add2 <br />\n";
     }

      if(isset($_POST['postcode'])){
        $postcode = $_POST['postcode'];
        echo "$postcode <br />\n";
      }

      if(isset($_POST['email'])){
        $email = $_POST['email'];
        echo "Contact Email Address $email <br />\n";
      }

      if(isset($_POST['telephone'])){
      $telephone = $_POST['telephone'];
      echo "Contact Telephone Number $telephone <br />\n";
      }

      if(isset($_POST['contact'])){
      $contact = $_POST['contact'];
      echo "You would like to be contacted by $contact <br />\n";
      }

      if(isset($_POST['trainers'])){
      $trainers = $_POST['trainers'];
      echo "The trainers you would like are $trainers <br />\n";
      }



      if(isset($_POST['extras'])){
      $extras = $_POST['extras'];
      echo "The extras you would like are $extras <br />\n";
      }



       $extraCost = 0;
       $trainerCost= 0;
       $totalCost= 0;
       $totalCostV= 0;

        $extra = $_POST['extras'];

        if (array_key_exists($extra, $extraValues)) {
             $extraCost = (float) $extraValues[$extra]; 
            echo "The cost of your extras are &pound; $extraCost<br />\n";
        } 


        $trainer = $_POST['trainers'];

        if (array_key_exists($trainer, $trainerValues)) { 
            $trainerCost = (float) $trainerValues[$trainer]; 
            echo "The cost of your Trainers are &pound; $trainerCost<br />\n";
        }

        $totalCost = round($extraCost+$trainerCost+$totalCost); 
      echo "The cost of your Trainers are (Excluding 20% tax) &pound; $totalCost<br />\n";


      $totalCostV = round(($extraCost+$trainerCost+$totalCost)*1.20,2); 
      echo "The cost of your Trainers are (including 20% tax) &pound; $totalCostV<br />\n";
    ?>
      </div>

      <!-- Holds the foorter information -->
      <div class="footer">
        <p>&#169; 2010 FeetFirst Uk Ltd </p>
        </div>

      </div> 

    </body> 

3 个答案:

答案 0 :(得分:1)

因为,在开头你的$ totalCost = 0.然后它应该运行正常:

$totalCost = round($extraCost+$trainerCost+$totalCost); 
echo "The cost of your Trainers are (Excluding 20% tax) &pound; $totalCost<br />\n";

$totalCostV = round($totalCost*1.20,2); // this line is edited
echo "The cost of your Trainers are (including 20% tax) &pound; $totalCostV<br />\n";

但我觉得它应该是这样的:

$totalCost = round($extraCost+$trainerCost); // this line is edited
echo "The cost of your Trainers are (Excluding 20% tax) &pound; $totalCost<br />\n";

$totalCostV = round($totalCost*1.20,2); // this line is edited
echo "The cost of your Trainers are (including 20% tax) &pound; $totalCostV<br />\n";

答案 1 :(得分:0)

$totalCost = 0
...
$totalCost = round($extraCost+$trainerCost+$totalCost);
=> 60
...

$totalCostV = round(($extraCost+$trainerCost+$totalCost)*1.20,2);
            = round(($extraCost+$trainerCost+$extraCost+$trainerCost)*1.20,2);
            = round(($extraCost+$trainerCost)*2*1.20,2);
            = 144

我认为+$totalCost

中有额外的$totalCostV

答案 2 :(得分:0)

可能是转换问题。听起来像是计算* 2而不是* 1.2。 您可以尝试在这些calucations之前进行(浮动)转换。

因为JS浮点数非常不准确,所以以美分计算所有内容,然后在输出时除以100也是一个想法。