Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ How much is "((1 + 2 + $var1) * $var2) / 2 - 5"?
How much is "((1 + 2 + $var1) * $var2) / 2 - 5"?
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Simy202
(1 post) Bio
|
Date
| Tue 25 Jan 2011 08:20 AM (UTC) Amended on Tue 25 Jan 2011 08:21 AM (UTC) by Simy202
|
Message
| I'm going through a php tutorial right now and this [url=http://www.phpkode.com/scripts/category/php-math/]PHP Math[/url] example below is driving me nuts. The exmaple was "((1 + 2 + $var1) * $var2) / 2 - 5", and all the rest of the code below is just me trying to figure it out.
The answer ends up being "7", but to me it looks like PHP is sayding "24 / -3 = 7" when it should be "24 / -3 = -8". What am I missing in this PHP Math issue?? Thanks!
<?php
$var1 = 3;
$var2 = 4;
?>
$var1 = 3;<br />
$var2 = 4;<br /><br />
<br />
<h1> ((1 + 2 + $var1) * $var2) / 2 - 5 = <?php echo((1 + 2 + $var1) * $var2) / 2 - 5; ?><br /> </h1>
<hr />
<br />
<br />
LEFT SIDE:<br />
(1 + 2 + $var1) * $var2 = <?php echo ((1 + 2 + $var1) * $var2); ?><br />
<br />
RIGHT SIDE:<br />
2 - 5 = <?php echo 2 - 5; ?><br />
<br />
24 / -3 SHOULD BE:
<h1>24 / -3 = <?php echo 24 / -3; ?><br /></h1>
That spits out:
$var1 = 3;
$var2 = 4;
((1 + 2 + $var1) * $var2) / 2 - 5 = 7
LEFT SIDE:
(1 + 2 + $var1) * $var2 = 24
RIGHT SIDE:
2 - 5 = -3
24 / -3 SHOULD BE:
24 / -3 = -8 | Top |
|
Posted by
| Nick Gammon
Australia (23,140 posts) Bio
Forum Administrator |
Date
| Reply #1 on Tue 25 Jan 2011 08:31 AM (UTC) Amended on Tue 25 Jan 2011 09:54 AM (UTC) by Nick Gammon
|
Message
|
Quote:
$var1 = 3;
$var2 = 4;
((1 + 2 + $var1) * $var2) / 2 - 5 = 7
Substituting:
((1 + 2 + 3) * 4) / 2 - 5
= (6 * 4) / 2 - 5
= 24 / 2 - 5
= 12 - 5
= 7
You divide before you subtract. :-) |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #2 on Tue 25 Jan 2011 08:33 AM (UTC) Amended on Tue 25 Jan 2011 08:34 AM (UTC) by Twisol
|
Message
| First glance: you have to deal with the order of operations. "a / 2 - 5" (where I'm replacing everything else with 'a' for the moment) is a divided by 2, then subtracted by 5. But you suggest in your explanation that you want a / -3, right? So you need a / (2 - 5); the parentheses change the order of operations, causing the subtraction to occur before the division.
[EDIT]: Nick's post above explains why you get 7 right now, and my post explains what you need to do to get -8. :) |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
13,326 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top