I agree on the eval() idea but yu have to keep in mind what you are going to do when the (editable) formula changes from
d = a + b 1.1
to
d = a + b - c 1.1
Your PHP function would have to be rewritten.
Okay if you're the esoteric type you could to something like (pseudocode):
function create_function($forumula_id) {
// fetch formula with given id from DB:
$formula = SELECT formula FROM database WHERE id=$formula_id
// now $formula would be something like '$d = $a + $b - $c * 1.1'
// find all necessary variables in formula:
$array_variables = some regular expression searching for "(\$[a-zA-Z]*)", returning matches
// now we know which variables we need, let's build a php function inside a string
$function = "function tmpfunc($formula";
// add parameters to the function for every variable in the formula
foreach ($array_variables as $variable) {
$function .= ", $variable";
}
// finish the header
$function .= ") {";
// now perform the formula
$function .= '$returnvalue = eval($formula);';
// and return the result, close the function
$function .= "return $returnvalue;}";
// now we have build the function, lets call it
$result = eval($function);
}
// end pseudocode
Now I'm pretty sure this will work but the code above probably has some errors in it.
What I basically do is to grab the formula, look what variables I need and then I generate a PHP function on-the-fly that takes all necessary arguments and performs the calculation.
Thereby I circumvent the problem of functions being unaware of additional variables in changed formulas. By the way, you only need one create_function() function, it should be able to handle any mathematical operation that PHP is capable of. Heck you even could use PHP functions inside the formula... okok I quit here.
I hope I didn't raise more questions than I answered 🙂)
Good Luck and let me know what solution you chose when you're done!
Dominique