可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
if ($baz == 0.0) {
echo \'Divisor is 0\';
} else {
...
}
Rather than use eval, which is highly dangerous if you\'re using user-input within the evalled expression, why not use a proper parser such as evalmath on PHPClasses, and which raises a clean exception on divide by zero
回答2:
Here\'s another solution:
<?php
function e($errno, $errstr, $errfile, $errline) {
print \"caught!\\n\";
}
set_error_handler(\'e\');
eval(\'echo 1/0;\');
See set_error_handler()
回答3:
You just need to set an error handler to throw an exception in case of errors:
set_error_handler(function () {
throw new Exception(\'Ach!\');
});
try {
$result = 4 / 0;
} catch( Exception $e ){
echo \"Divide by zero, I don\'t fear you!\".PHP_EOL;
$result = 0;
}
restore_error_handler();
回答4:
As others have mentioned, consider trying a solution that will let you check if the denominator is 0.
Since that advice seems useless your purpose, here\'s a little background on PHP error handling.
Early versions of PHP didn\'t have exceptions. Instead, error messages of various levels were raised (Notices, Warnings, Etc). A Fatal error stops execution.
PHP5 brought exceptions to the table, and newer PHP provided libraries (PDO) will throw exceptions when bad/unexpected things happen. Hoever, the core codebase was NOT rewritten to use exception. Core functions and operations still rely on the old error system.
When you divide by 0, you get a Warning, not an exception
PHP Warning: Division by zero in /foo/baz/bar/test.php(2) : eval()\'d code on line 1
PHP Stack trace:
PHP 1. {main}() /foo/baz/bar/test.php:0
PHP 2. eval() /foo/baz/bar/test.php:2
If you want to \"catch\" these, you\'ll need to set a custom error handler that will detect division by zero errors and do something about them. Unfortunately, custom error handlers are a catch all, which means you\'ll also need to write some code to do something appropriate with all other errors.
回答5:
if(@eval(\"\\$result = $expresion;\")===FALSE){
$result=0;
}
Won\'t just catch divide by 0 errors though.
回答6:
I was facing that problem as well (dynamic expressions). Idid it that way which might not be the nicest way but it works. Instead of throwing an Exception you can of course return null or false or whatever you wish. Hope this helps.
function eval_expression($expression)
{
ob_start();
eval(\'echo (\' . $expression . \');\');
$result = ob_get_contents();
ob_end_clean();
if (strpos($result, \'Warning: Division by zero\')!==false)
{
throw new Exception(\'Division by zero\');
}
else return (float)$result;
}
回答7:
On PHP7 you can use DivisionByZeroError
try {
echo 1/0;
}
catch(DivisionByZeroError $e){
echo \"got $e\";
}
回答8:
You can simply catch DivisionByZeroError
in PHP >= 7
See http://php.net/manual/en/class.divisionbyzeroerror.php
回答9:
Use a @
(An error control operator.) This tells php to not output warnings in case of errors.
eval(\"\\$result = @($expresion);\");
if ($result == 0) {
// do division by zero handling
} else {
// it\'s all good
}
回答10:
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).
回答11:
Problem:
b=1; c=0;
a=b/c;
// Error Divide by zero
Solution simple:
if(c!=0) a=b/c;
else // error handling
回答12:
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:
// Since set_error_handler doesn\'t catch Fatal errors, we do this
function shutdown()
{
$lastError = error_get_last();
if (!empty($lastError)) {
$GLOBALS[\'logger\']->debug(null, $lastError);
}
}
register_shutdown_function(\'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.
回答13:
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.
// You may need to do a better parsing than this to tokenize your expression.
// In PHP, you could for example use token_get_all()
$formula = explode(\' \', \'foo + bar * ( baz / ( foz - bak ) )\');;
$queue = array();
$operators = array();
$precedence = array(\'-\' => 2, \'+\' => 2, \'/\' => 3, \'*\' => 3, \'^\' => 4);
$rightAssoc = array(\'^\');
$variables = array(\'foo\' => $foo, \'bar\' => $bar, \'baz\' => $baz, \'foz\' => $foz, \'bak\' => $bak);
foreach($formula as $token) {
if(isset($variables[$token])) {
$queue[] = $variables[$token];
} else if(isset($precedence[$token])) {
// This is an operator
while(
sizeof($operators) > 0 &&
$operators[sizeof($operators)-1] != \'(\' && (
$precedence[$operators[sizeof($operators)-1]] > $precedence[$token] ||
(
$precedence[$operators[sizeof($operators)-1]] == $precedence[$token] &&
!in_array($operators[sizeof($operators)-1], $rightAssoc)
)
)
) $queue[] = array_pop($operators);
$operators[] = $token;
} else if($token == \'(\') {
$operators[] = \'(\';
} else if($token == \')\') {
while($operators[sizeof($operators)-1] != \'(\') {
$queue[] = array_pop($operators);
}
array_pop($operators);
} else if($token == \')\') {
while($operators[sizeof($operators)-1] != \')\') {
$queue[] = array_pop($operators);
}
if(null === array_pop($operators))
throw new \\Exception(\"Mismatched parentheses\");
}
$queue = array_merge($queue, array_reverse($operators));
$stack = array();
foreach($queue as $token) {
if(is_numeric($token)) $stack[] = $token;
else switch($token) {
case \'+\' :
$stack[] = array_pop($stack) + array_pop($stack);
break;
case \'-\' :
// Popped variables come in reverse, so...
$stack[] = -array_pop($stack) + array_pop($stack);
break;
case \'*\' :
$stack[] = array_pop($stack) * array_pop($stack);
break;
case \'/\' :
$b = array_pop($stack);
$a = array_pop($stack);
if($b == 0)
throw new \\Exception(\"Division by zero\");
$stack[] = $a / $b;
break;
}
}
echo \"The result from the calculation is \".array_pop($stack).\"\\n\";
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:
eval(\"$foo + $bar * ( $baz / ( $foz - $bak ) )\");
becomes
function custom_division($a, $b) { if($b == 0) throw Exception(\"Div by 0\"); }
eval(\"$foo + $bar * ( custom_division( $baz, ( $foz - $bak ) )\");