function evaluate_equation ($equation, $values
{
if (is_array$equation))
{
$operator = array_shift$equation);
switch ($operator
{
case '+'
return evaluate_equation$equation[0], $values)+evaluate_equation$equation[1], $values);
case '-'
return evaluate_equation$equation[0], $values)-evaluate_equation$equation[1], $values);
case '*'
return evaluate_equation$equation[0], $values)*evaluate_equation$equation[1], $values);
case '/'
return evaluate_equation$equation[0], $values)/evaluate_equation$equation[1], $values);
case '%'
return evaluate_equation$equation[0], $values)%evaluate_equation$equation[1], $values);
case '^'
return powevaluate_equation$equation[0], $values), evaluate_equation$equation[1], $values));
case 'neg'
return evaluate_equation$equation[0], $values)*-1;
case 'abs'
return absevaluate_equation$equation[0], $values));
case 'sqrt'
return sqrtevaluate_equation$equation[0], $values));
case 'log'
return logevaluate_equation$equation[0], $values), evaluate_equation$equation[1], $values));
case 'ln'
return logevaluate_equation$equation[0], $values));
case 'exp'
return expevaluate_equation$equation[0], $values));
case '!'
return 0;
case 'root'
return powevaluate_equation$equation[0], $values), 1/evaluate_equation$equation[1], $values));
// trig
case 'sin'
return sinevaluate_equation$equation[0], $values));
case 'cos'
return cosevaluate_equation$equation[0], $values));
case 'tan'
return tanevaluate_equation$equation[0], $values));
case 'sec'
return 1/cosevaluate_equation$equation[0], $values));
case 'csc'
return 1/sinevaluate_equation$equation[0], $values));
case 'cot'
return 1/tanevaluate_equation$equation[0], $values));
// hyperbolic trig
case 'sinh'
return sinhevaluate_equation$equation[0], $values));
case 'cosh'
return coshevaluate_equation$equation[0], $values));
case 'tanh'
return tanhevaluate_equation$equation[0], $values));
case 'sech'
return 1/coshevaluate_equation$equation[0], $values));
case 'csch'
return 1/sinhevaluate_equation$equation[0], $values));
case 'coth'
return 1/tanhevaluate_equation$equation[0], $values));
// arc trig
case 'arcsin'
return asinevaluate_equation$equation[0], $values));
case 'arccos'
return acosevaluate_equation$equation[0], $values));
case 'arctan'
return atanevaluate_equation$equation[0], $values));
// inverse hyperbolic trig
case 'arsinh'
return asinhevaluate_equation$equation[0], $values));
case 'arcosh'
return acoshevaluate_equation$equation[0], $values));
case 'artanh'
return atanhevaluate_equation$equation[0], $values));
default
throw new Exception'usupported operator '$operator);
}
}
else
{
if (ctype_alpha$equation))
{
if (isset$values$equation]))
{
return $values$equation];
}
else
{
throw new Exception'equation contains an unknown variable ''$equation''!');
}
}
else
{
return $equation
}
}
}