Click to See Complete Forum and Search --> : php calculation problem


mvnr_83
07-23-2008, 12:47 AM
Hi all,

I have a strange problem while calculating using a formula.

Here is my code
<?php
$num = 1000;
$num2 = 10;
$num3 = 50;

$formula = '(A1*A2)+A3';
$formula = str_replace('A1',$num,$formula);
$formula = str_replace('A2',$num2,$formula);
$formula = str_replace('A3',$num3,$formula);
echo $formula;
?>

i need the value of formula as 10050
The formula comes from database and it is in string format. it is not possible to remove quotes to $formula.
Please help me

Thanks

NogDog
07-23-2008, 03:01 AM
OK, so when I run your code, it outputs (1000*10)+50. If you want it to actually do something, you'll probably have to use eval (http://www.php.net/eval)().

eval('echo ' . $formula . ';');

However, as the saying goes, "If eval() is the answer, you're probably asking the wrong question."