 <p>You could use the function <a href="http://www.php.net/manual/en/function.eval.php" rel="nofollow">eval</a>, but this is very dangerous !</p> <p>This class might help you : <a href="http://www.phpclasses.org/browse/package/2695.html" rel="nofollow">http://www.phpclasses.org/browse/package/2695.html</a></p> <ul> <li>This class can be used to safely evaluate mathematical expressions.</li> <li>The class can take an expression in a text string and evaluate it by replacing values of variables and calculating the results of mathematical functions and operations.</li> <li>It supports implicit multiplication, multivariable functions and nested functions.</li> <li><strong>It can be used to evaluate expressions from untrusted sources. It provides robust error checking and only evaluates a limited set of functions.</strong></li> <li>It could be used to generate graphs from expressions of formulae. </li> </ul>