Jump to content

Recommended Posts

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 }
?>

 

Link to comment
https://forums.phpfreaks.com/topic/158576-solved-maths-question/
Share on other sites

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.

<?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 ;)

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.