affc Posted May 18, 2009 Share Posted May 18, 2009 Gday, Even though I am starting to get the hand of PHP i am not much when it comes to maths, so I am hoping someone else is a little more gifted in this area I want to find out the difference between a number that is submitted and a number in a database and work out the difference in a positive value. Eg if 6 was in the database and 14 was submitted, the difference would be 8. Im thinking something like <?php if($submitted>$database){ $submitted-$database=$result }else if($submitted<$database){ $database-$submitted=$result } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/ Share on other sites More sharing options...
KevinM1 Posted May 18, 2009 Share Posted May 18, 2009 Gday, Even though I am starting to get the hand of PHP i am not much when it comes to maths, so I am hoping someone else is a little more gifted in this area I want to find out the difference between a number that is submitted and a number in a database and work out the difference in a positive value. Eg if 6 was in the database and 14 was submitted, the difference would be 8. Im thinking something like <?php if($submitted>$database){ $submitted-$database=$result }else if($submitted<$database){ $database-$submitted=$result } ?> It'd be easier to just use the absolute value: $tmp = $submitted - $database; //store the difference in a temporary variable $result = abs($tmp); //find the absolute value of the difference and store the result echo "$result"; From what I can tell by reading the manual, the abs() function can't calculate the absolute value of a mathematical expression; it can only find the value of individual numbers. That's why you should first store the difference in a temporary variable. Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836345 Share on other sites More sharing options...
affc Posted May 18, 2009 Author Share Posted May 18, 2009 What is the submitted value is 4 and the database number is 50, that would give -54, how does this ABS turn it into +54 so I dont have the minus in it or does it do it automatically? thanx Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836349 Share on other sites More sharing options...
affc Posted May 18, 2009 Author Share Posted May 18, 2009 Actually my maths is really bad, the result would be -46 lol but still would it find out that the difference should be 54? Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836350 Share on other sites More sharing options...
affc Posted May 18, 2009 Author Share Posted May 18, 2009 Just tried it like this and worked a charm, but i dont really understand how it finds out the difference lol, it even works if I put -10, thanx Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836354 Share on other sites More sharing options...
affc Posted May 18, 2009 Author Share Posted May 18, 2009 <?php $database="10"; $submitted="-10"; $tmp = $submitted - $database; //store the difference in a temporary variable $result = abs($tmp); //find the absolute value of the difference and store the result echo "$result"; ?> Result was 20 which is perfect so obviously works some how Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836357 Share on other sites More sharing options...
KevinM1 Posted May 18, 2009 Share Posted May 18, 2009 Absolute value is simply the value of a number. Whether it's positive or negative doesn't matter - the value of the number is the same in either case. That's what the abs() function returns. More info on absolute values: http://en.wikipedia.org/wiki/Absolute_value Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836360 Share on other sites More sharing options...
affc Posted May 18, 2009 Author Share Posted May 18, 2009 Thanx once again I also noticed my status is now Enthusiast No Guru yet Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836364 Share on other sites More sharing options...
Daniel0 Posted May 18, 2009 Share Posted May 18, 2009 Thanx once again I also noticed my status is now Enthusiast No Guru yet Heh... sorry to let you down, but "Guru" is manually assigned to people who've shown themselves worthy. You can find more info here: http://www.phpfreaks.com/forums/index.php/topic,232740.0.html Quote Link to comment https://forums.phpfreaks.com/topic/158576-solved-maths-question/#findComment-836367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.