Расчет по формуле php

function calculated_formula($elements_formula=array(), $formula='',$default=0){
	if(count($elements_formula)>0 && strlen($formula)>0){		
		$pattern = "/\[([A-Za-z_0-9]*?)\]/i";
		preg_match_all($pattern, $formula, $matches);
		foreach($matches[1] as $elemFormula){
			if(!isset($elements_formula[$elemFormula]) || empty($elements_formula[$elemFormula])){
				$elements_formula[$elemFormula] = $default;
			}
		}
		$replacement = "\$elements_formula$0";
		$result = preg_replace($pattern, $replacement, $formula);
		return eval("return $result;");
	}
}
$test['test1'] = '';
$test['test2'] = 3;
echo calculated_formula($test, '[test1]+[test2]+1',1);