I have a large mathematical expression that has to be created dynamically. For example, once I have parsed "something" the result will be a string like: "$foo+$bar/$baz";
.
So, for calculating the result of that expression I'm using the eval
function... something like this:
eval("\$result = $expresion;");
echo "The result is: $result";
The problem here is that sometimes I get errors that says there was a division by zero, and I don't know how to catch that Exception. I have tried things like:
eval("try{\$result = $expresion;}catch(Exception \$e){\$result = 0;}");
echo "The result is: $result";
Or:
try{
eval("\$result = $expresion;");
}
catch(Exception $e){
$result = 0;
}
echo "The result is: $result";
But it does not work. So, how can I avoid that my application crashes when there is a division by zero?
Edit:
First, I want to clarify something: the expression is built dynamically, so I can't just eval if the denominator is zero. So... with regards to the Mark Baker's comment, let me give you an example. My parser could build something like this:
"$foo + $bar * ( $baz / ( $foz - $bak ) )"
The parser build the string step by step without worrying about the value of the vars... so in this case if $foz == $bak
there's in fact a division by zero: $baz / ( 0 )
.
On the other hand as Pete suggested, I tried:
<?php
$a = 5;
$b = 0;
if(@eval(" try{ \$res = $a/$b; } catch(Exception \$e){}") === FALSE)
$res = 0;
echo "$res\n";
?>
But it does not print anything.
A string containing numbers and the mathematical operators + - * / is passed as input. The program must evaluate the value of the expression (as per BODMAS) and print the output.
Example Input/Output: If the argument is "7 + 4*5" the output must be 27. If the argument is "55 + 21 * 11 - 6/0" the output must be "error" (As division by zero is not defined).
I realize this is an old question, but it is relevant today and I don't really like the answers here.
The proper way to fix this, is by actually evaluating the expression yourself - that is, by parsing the expression, then evaluating it step by step, instead of by transpiling it to PHP. This can be done using the https://en.wikipedia.org/wiki/Shunting-yard_algorithm.
I wrote the following implementation, but I haven't tested it. It's based on the above Wikipedia article. There is no support for right-associative operators, so it's slightly simplified.
In your particular case
Even though I would prefer the Shunting Yard solution - if I still decided to go for an eval()-version, I would create a custom_division($leftHandSide, $rightHandSide) method, that throws an exception. This code:
becomes
Won't just catch divide by 0 errors though.
Problem:
b=1; c=0; a=b/c; // Error Divide by zero
Solution simple:
I've been struggling with this too, the
set_error_handler
solutions were not working for me, probably based on PHP version differences.The solution for me was to attempt to detect an error on shutdown:
I'm not sure why a divide by 0 is shutting down rather than being handled by the
set_error_handler
but this helped me get beyond it just silently dying.Here's another solution:
See
set_error_handler()