Jump to content

Recommended Posts

Posted

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;

 

>echo 10 + (2.4 * $number1) + (5.7 * $number2) - ( * $number3) * $number4;}

 

I have looked over PHP.net but cant find anything that quite covers what I am trying to accomplish.

 

Any guidance as to proper syntax would be of great help.

Posted

The last half of your formula is not valid code:

>( * $number3) * $number4;}

- You're indicating PHP should multiply something times $number3, but that something is missing from the formula.

 

- 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).

 

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.

Posted

Sorry,

 

Should be

>echo 10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3) * $number4;

 

I have created a form that has;

 

>Enter first number: <input type="text" name="number1">
Enter second number: <input type="text" name="number2">
Enter third: <input type="text" name="number3">
<br /><br />
Choose fourth:
<select name="number4">
<option value="1.2">blah</option>
<option value="1.3">blah1</option>
<option value="1.5">blah2</option>
<option value="1.7">blah3</option>
<option value="1.9">blah4</option>

 

I want the PHP code to process the numbers in the order described above.

 

If the above is not valid what about;

 

>$var1 = 2.4 * $number1
$var2 = 5.7 * $number2
$var3 = 10 * $number3
$var4 = 10 + $var1 + $var2 - $var3 * $number4

echo $var4

 

Thanks for working with me David, I am new to the PHP mathmatic functions.

Posted

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?

 

Just a guess here, but your formula may not be handling the multiplication of $number4 the way you want.

 

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:

 

1) Anything within parenthesis is evaluated first

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.

 

If you use the following number as an example:

>$number1=1
$number2=2
$number3=3
$number4=1.2

...and substitute them into your formula:

>echo 10 + (2.4 * 1) + (5.7 * 2) - (10 * 3) * 1.2;

The first thing PHP will do is evaluate the multiplications in parenthesis:

>echo 10 + 2.4 + 11.4 - 30 * 1.2;

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:

>echo 10 + 2.4 + 11.4 - 36;

Finally, it will evaluate the addition and subtraction operations from left to right:

>echo -12.2;

If you want to multiply the entire sum of 10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3) 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:

>echo (10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3)) * $number4;

With the extra set of parenthesis to alter the order of operations, the above formula would evaluate to '-7.44' instead of '-12.2'.

 

Hope this helps...

Posted
If you want to multiply the entire sum of 10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3) 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:

>echo (10 + (2.4 * $number1) + (5.7 * $number2) - (10 * $number3)) * $number4;

With the extra set of parenthesis to alter the order of operations, the above formula would evaluate to '-7.44' instead of '-12.2'.

 

Hope this helps...

 

That's exactly what I wanted and it works perfectly. Thanks David.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...