Jump to content

[SOLVED] Maths Question


affc

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

Archived

This topic is now archived and is closed to further replies.

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