From The Blog

PHP: Calculate result of a mathematical expression given in a string

10 September 2012, in PHP

How to calculate the result of a mathematical expression given as a string:

function calc_string( $mathString )
{
$cf_DoCalc = create_function("", "return (" . $mathString . ");" );
return $cf_DoCalc();
}

echo "result is: "calc_string("2+25*3");

//result is: 77



Post a Comment