php表单计算器如何解决这个问题

OK i am making a form that calculates line 1+ line 2 +*.01% then adds line 3+ line 4 then gives a total. I am having a problem with *.01 it works with just the first two but when i add the last two it drops one line if i take it out all four add fine.

<?php
if (count($_POST) > 0 && isset($_POST["calc15TextArea2"])){
    // add First and Second Numbers
    $sum = $_POST["indexEditbox1"] + $_POST["indexEditbox2"]*.01+ $_POST      ["calc15Editbox3"] + $_POST ["calc15Editbox4"];
    // their sum is diplayed as
    echo "Total is $ $sum";
}
?>

wrap all your post vars in intval() like eg intval($_POST['indexEditBox2'])

this function will get the integer value out of the string. (use the equivalent functions such as floatval() if they are not integers)

edit: Also try 0.01 in stead of .01. Given that this just is a wild hunch, I just might be that the .. is picked up as the concatenation operator.

so try:

    $sum = floatval( $_POST["indexEditbox1"] ) + floatval( $_POST["indexEditbox2"] ) * 0.01+ floatval( $_POST["calc15Editbox3"] ) + floatval( $_POST["calc15Editbox4"] ) ;

edit2: See the comments! multiplications and divisions take precedence over adding and subtracting.

If you are using html 5 you can get around using floatval and intval by specify the input type on your form inputs in your html markup.

An example would be;

<input type="number" name="indexEditBox1" />

Then in your php code you could simply do;

$sum = $_POST["indexEditbox1"] + $_POST["indexEditbox2"] * .01 + $_POST["calc15Editbox3"] + $_POST ["calc15Editbox4"];

Also I would recommend that you wrap your calculations in parenthesis to get the desired outcome, it also makes it easier to read especially as your calculations get more complex. I would no change it to;

$sum = (($_POST["indexEditbox1"] + $_POST["indexEditbox2"]) * .01) 
       + ($_POST["calc15Editbox3"] + $_POST ["calc15Editbox4"]);

If you are not using html5 or feel the need to be secure as people can inject data into forms you should use the intval or floatval functions.