Math Using Php

liunx

Guest
I am working on a small input form that requires users to input four numbers. The output utilizes PHP to add, subtract and multiply those numbers in a certain order. The problem I am having is that the outcome is not valid. The PHP part of the script is;<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo 10 + (2.4 * $number1) + (5.7 * $number2) - ( * $number3) * $number4;}<!--c2--></div><!--ec2--><br /><br />I have looked over PHP.net but cant find anything that quite covers what I am trying to accomplish.<br /><br />Any guidance as to proper syntax would be of great help.<!--content-->
The last half of your formula is not valid code:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->( * $number3) * $number4;}<!--c2--></div><!--ec2--><br />- You're indicating PHP should multiply something times $number3, but that something is missing from the formula.<br /><br />- The closing brace character at the end ("}") doesn't appear to have a corresponding open brace, unless this statement is the end of an 'if' block (for example).<br /><br />If that's not enough help, if you can describe what you want the formula to do, I can tell you how it should be coded to do that.<!--content-->
Sorry,<br /><br />Should be <!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo 10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3) * $number4;<!--c2--></div><!--ec2--><br /><br />I have created a form that has;<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->Enter first number: <input type="text" name="number1"><br />Enter second number: <input type="text" name="number2"><br />Enter third: <input type="text" name="number3"><br /><br /><br /><br />Choose fourth:<br /><select name="number4"><br /><option value="1.2">blah</option><br /><option value="1.3">blah1</option><br /><option value="1.5">blah2</option><br /><option value="1.7">blah3</option><br /><option value="1.9">blah4</option><!--c2--></div><!--ec2--><br /><br />I want the PHP code to process the numbers in the order described above.<br /><br />If the above is not valid what about;<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$var1 = 2.4 * $number1<br />$var2 = 5.7 * $number2<br />$var3 = 10 * $number3<br />$var4 = 10 + $var1 + $var2 - $var3 * $number4<br /><br />echo $var4<!--c2--></div><!--ec2--><br /><br />Thanks for working with me David, I am new to the PHP mathmatic functions.<!--content-->
That helps, but I'm still not sure how you want the calculation to occur. For example, given specific values for $number1, $number2, $number3, and $number4, what result should your formula produce?<br /><br />Just a guess here, but your formula may not be handling the multiplication of $number4 the way you want.<br /><br />There is a process known as 'order of operations', which determine which math operations will be performed before other ones. In your formula, there are two aspects of 'order of operations' that would determine how the formula is calculated:<br /><br />1) Anything within parenthesis is evaluated first<br />2) Within a group of parenthesis, or within the whole formula after all parenthesis have been evaluated, multiplication and division is performed first, then addition and subtraction is performed, both from left to right.<br /><br />If you use the following number as an example:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$number1=1<br />$number2=2<br />$number3=3<br />$number4=1.2<!--c2--></div><!--ec2--><br />...and substitute them into your formula:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo 10 + (2.4 * 1) + (5.7 * 2) - (10 * 3) * 1.2;<!--c2--></div><!--ec2--><br />The first thing PHP will do is evaluate the multiplications in parenthesis:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo 10 + 2.4 + 11.4 - 30 * 1.2;<!--c2--></div><!--ec2--><br />Next, it will look for multiplication and division operations to perform, from left to right and evaluate them. It will find '30 * 1.2' and evaluate it:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo 10 + 2.4 + 11.4 - 36;<!--c2--></div><!--ec2--><br />Finally, it will evaluate the addition and subtraction operations from left to right:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo -12.2;<!--c2--></div><!--ec2--><br />If you want to multiply the entire sum of <!--fonto:Courier--><span style="font-family:Courier"><!--/fonto-->10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3)<!--fontc--></span><!--/fontc--> times $number4, then that entire expression would need to be enclosed in parenthesis, forcing PHP to evaluate that entire sum first before multiplying it by $number4:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo (10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3)) * $number4;<!--c2--></div><!--ec2--><br />With the extra set of parenthesis to alter the order of operations, the above formula would evaluate to '-7.44' instead of '-12.2'.<br /><br />Hope this helps...<!--content-->
<!--QuoteBegin-TCH-David+Sep 9 2005, 03:32 AM--><div class='quotetop'>QUOTE(TCH-David @ Sep 9 2005, 03:32 AM)</div><div class='quotemain'><!--QuoteEBegin-->If you want to multiply the entire sum of <!--fonto:Courier--><span style="font-family:Courier"><!--/fonto-->10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3)<!--fontc--></span><!--/fontc--> times $number4, then that entire expression would need to be enclosed in parenthesis, forcing PHP to evaluate that entire sum first before multiplying it by $number4:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo (10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3)) * $number4;<!--c2--></div><!--ec2--><br />With the extra set of parenthesis to alter the order of operations, the above formula would evaluate to '-7.44' instead of '-12.2'.<br /><br />Hope this helps...<br /><div align="right"><a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=148094"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />That's exactly what I wanted and it works perfectly. Thanks David.<!--content-->
 
Back
Top