Page 1 of 1

variables in math question

Posted: Mon Dec 27, 2004 5:14 pm
by Pineriver
Hello,
I am wondering if it is all possible to have a variable operate a math command like...

$set = '+';//$set comes from a database and it changes

echo (10 $set 4); // would return 14

This doesnt seem to work so I am wondering if I would have to do

if($set == '+'){ echo (10 + 4); }

if anyone can figure out how to do that other way please share, thanks.
It would be a big time saver since there are 6 of these

Posted: Mon Dec 27, 2004 5:20 pm
by Robert Plank

Code: Select all

$set = '+';//$set comes from a database and it changes 
echo eval("return 10 $set 4;");

Posted: Tue Dec 28, 2004 4:48 am
by timvw
there are only 2 things you need to know:

validation, so people don't request evil things from your string
evaluation of the expression

a very dirty sample can be found at
http://home.mysth.be/~timvw/programming ... ulator.txt