Performing maths operations on PHP variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noelinho
Forum Newbie
Posts: 3
Joined: Wed Jul 16, 2008 7:09 am

Performing maths operations on PHP variables

Post by Noelinho »

I am writing a script that performs some simple mathematics (adding, subtracting, dividing and multiplying). I started by declaring some values that could be used (up to six, $a, $b, $c, $d, $e, $f). Users can then manipulate these functions by using simple commands, such as:

Code: Select all

($a - $b)
.

So, in this case, if $a = 6 and $b = 3, it would return the value 3. This works without a problem when everything is pre-defined. However, the actual sum that is delivered is coming via a form, as follows:

Code: Select all

$sum = $_POST['calculation'];
However, as hard as I try, no matter what I try to do, I can only get PHP to print out the input into the form field. I have not been able to make it actually perform the maths. How do I do this?

What is entered into the form is the following:

Code: Select all

a - b
This is for the ease of use of the person performing the maths. I then use str_replace to convert to variables, so the variable ends up like this:

Code: Select all

$a - $b
The variables are, in turn, transferred into their relevant numbers, so in this case, if $a =6 and $b = 3, it ends up like this:

Code: Select all

6 - 3
The answer to this sum is, obviously, 3, meaning I have the following variable:

Code: Select all

$sum = 6 - 3
But what I actually want is for PHP to process this, which I haven't been able to work out, despite searching for a solution to this for hours. I've tried using eval(), but haven't got anywhere, and I'm not really sure I want to use eval() anyway, because this page can be accessed by anyone.

Any help would be greatly appreciated. Is it something to do with quotes? In essence, I want PHP to process the following:

Code: Select all

$sum = 6 - 3;
Whereas, from the variable, at the moment, it is processing:

Code: Select all

$sum = "6 - 3";
So really, I think I'm trying to get rid of the quotes. Please let me know if I've missed any important information out.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Performing maths operations on PHP variables

Post by jaoudestudios »

Can you post the actual code you are using!
Noelinho
Forum Newbie
Posts: 3
Joined: Wed Jul 16, 2008 7:09 am

Re: Performing maths operations on PHP variables

Post by Noelinho »

The code, as requested:

Code: Select all

<?php
 
$a = 2;
$b = 5;
$c = 9;
$d = 6;
$e = 8;
$f = 25;
$g = 426;
 
$string = $_POST['calculation'];
$realscore = $string;
echo $realscore;
 
$score = $_POST['calculation'];
$score = str_replace("a", "$a", "$score");
$score = str_replace("b", "$b", "$score");
$score = str_replace("c", "$c", "$score");
$score = str_replace("d", "$d", "$score");
$score = str_replace("e", "$e", "$score");
$score = str_replace("f", "$f", "$score");
 
//((($a * $b ) + $d) * $f) + $c + $e;
 
 
 
 
echo "Your numbers are ".$a.", ".$b.", ".$c.", ".$d.", ".$e." and ".$f.".<br/>\n";
echo "Your target number is ".$g.".<br/>\n";
//if (isset($score)) { eval ("The number you made was ".$score."<br/>\n") }
 
//echo eval ("\$score;");
//echo $score;
?>
 
<form name="countdown" action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" name="calculation" />
<input type="submit" value="Submit">
</form>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Performing maths operations on PHP variables

Post by Eran »

I'm assuming the '$g' parameter is the result of this commented out calculation:

Code: Select all

 
//((($a * $b ) + $d) * $f) + $c + $e;
 
Cast your numbers into integers / floats to force them to become numbers (they are strings, which should work but apparently doesn't).

Code: Select all

 
$g = ( ( (int) $a * (int) $b ) + (int) $d ) + (int) $c + (int) $e; 
 
Also, don't use eval in PHP. It's bad practice
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Performing maths operations on PHP variables

Post by onion2k »

pytrin wrote:Also, don't use eval in PHP. It's bad practice
Actually this is one of the few times where it would be a good idea to use eval. Just check the incoming string isn't dodgy first - check that it only contains single letters between a and f, and basic maths operators. If it's safe then convert it to PHP variables by adding the $ signs and then use:

Code: Select all

$g = eval ("return $score");
...to get the evaluated value of the equation.
Noelinho
Forum Newbie
Posts: 3
Joined: Wed Jul 16, 2008 7:09 am

Re: Performing maths operations on PHP variables

Post by Noelinho »

Thanks onion2k, that's exactly what I was trying to do. It was the "return" part that I was missing. It now works very well, and I have put checking mechanisms in place to make sure no letters are left by the time the code is evaluated.

Thanks!
Post Reply