Friday, February 12, 2016

How to avoid errors using the function eval in php (under any framework)

Sometimes when you have to evaluate some mathematical expressions with eval it is difficult to control the errors even using try-catch. Here you have a trick to solve that problem:


$calculation = '1/0';

$result = @eval($calculation . "; return true;");
if($result) {
$rule2_val = doubleval(@eval($calculation));
}
else {
return 'Impossible to perform the calculation';
}

Hope helps!


No comments:

Post a Comment