Muddy_Funster Posted April 10, 2012 Share Posted April 10, 2012 Curious...If I store values and operators in a DB table and want to use them to perform calculations, how would I go about doing that? eg. Table has an "operator" column with |* 1.5|/ 0.75|+ 1.2| and I want to do something like desired_value = set_value affected by operator and value from table column, so if set_value = 10 I would want desired_value = 10 * 1.5 desired_value = 10 / 0.75 desired_value = 10 + 1.2 Anything like that possible? Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/ Share on other sites More sharing options...
trq Posted April 10, 2012 Share Posted April 10, 2012 eval. Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336003 Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Author Share Posted April 10, 2012 when I tried the following 3 options : $a = '* 1.2'; $b = '10'; $c = eval("$a ".$b); //$c = eval("$a $b"); //$c = eval($a ." $b"); echo $c; I got Parse error: syntax error, unexpected '*' in C:\xampp\htdocs\z.php(4) : eval()'d code on line 1 Does it not like math operators or am I doing it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336010 Share on other sites More sharing options...
trq Posted April 10, 2012 Share Posted April 10, 2012 What your evaluating needs to make sense. Your trying to execute * 1.210 What exactly do you expect to happen? Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336017 Share on other sites More sharing options...
AyKay47 Posted April 10, 2012 Share Posted April 10, 2012 $c = eval("$b ".$a); would make sense. Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336047 Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Author Share Posted April 10, 2012 OK, I'm clearly missing something: $a = '*2'; $b = '10'; //$c = eval("$b ". $a); = Unexpected $end //$c = eval("$b ". $a.";"); = var_dump($c) returns null //$c = eval("$b $a;"); = var_dump($c) returns null //$c = eval($b $a.";"); = Unexpected T_VARIABLE $d= $b.$a ; //$c = eval("$d;"); var_dump($c) = Null / var_dump($d) = string(4) "10*2" Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336078 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2012 Share Posted April 10, 2012 Eval() executes a (valid) php statement. If you wanted to produce and execute $c = 10 * 1.2;, that's what the "string" you supply to the eval() statement would need to be after all the $a, $b variables have been replaced with their contents. Edit: Specifically - <?php $a = '* 1.2'; $b = '10'; eval("\$c = $b $a;"); echo $c; Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336084 Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Author Share Posted April 10, 2012 Yeah, I thought I was doing that: $a = '*2'; $b = '10'; $d= $b.$a.";"; $e = 10*2; $c = eval("$d"); echo "<pre>"; var_dump($c); var_dump($d); var_dump($e); echo"</pre>"; Returns: NULL string(5) "10*2;" int(20) Like I said, I have to be missing something here because $e looks exactly like the var_dump of $d to my eyes Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336087 Share on other sites More sharing options...
trq Posted April 10, 2012 Share Posted April 10, 2012 Take another look at PFMaBiSmAd's example. Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336088 Share on other sites More sharing options...
Muddy_Funster Posted April 10, 2012 Author Share Posted April 10, 2012 Excelent! So I was just being completly stupid Note to self : assign the variable within the eval, don't assign the eval to the variable! Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336092 Share on other sites More sharing options...
xyph Posted April 10, 2012 Share Posted April 10, 2012 An alternate way to do this... keep in mind using EVAL can be dangerous. If you're going to allow user input, be sure to sanitize it AS STRICTLY AS POSSIBLE! <?php $a = 10; $b = '*2'; $c = eval('return '.$a.$b.';'); echo $c; ?> Check out the manual entry for eval. Under the 'Return Values' heading, it explains how eval() will populate any variables assigned by it. Quote Link to comment https://forums.phpfreaks.com/topic/260670-using-variable-as-math-operator/#findComment-1336131 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.