Division by zero, php - php

I have created this function that correctly displays the infinity symbol when trying to enter 0 into the Y text box. However I still get the division by zero error... here is my code.
here is the case for switch statment
case '/':
$prod = $x / $y;
break;
hh
//check for divide by 0
function check0($y)
{
if ($y == 0)
throw new Exception ($prod = "&#8734");
return FALSE;
}
try
{
check0($y);
$prod = $x /$y;
}catch(Exception $zero){
echo $zero->getMessage();
}

First: you are doing a division at the second line code (which can be devision by zero).
Second: no need to return false in your method since you are throwing an error.
Third: Why using an exception here and not just let you method return true of false and check on that before executing the devision.
Fourth: Why having a method if you only need to check on the value of $y. Calling the method or including an if-statement requires both just one line of code.
So, why can't it just be like:
case '/':
if($y > 0)
$prod = $x / $y;
break;

This is because you actually do the division near the top (in the case).

Your logic feels ... mmm well entangled.... Here have a look at this:
<?php
function divide($x, $y) {
if ( $y === 0 )
throw new Exception("Divide By Zero");
return $x/$y;
}
try {
$product = divide(3, 0);
}
catch(Exception $e) {
if ( $e->getMessage() == "Divide By Zero" )
$product = "&#8734";
else
$product = NULL;
}
print "Product is $product\n";
?>

You must call check0() before division. You should return FALSE if y == 0, TRUE otherwise.
If return TRUE then do division. However it's not necessary call a method to do this!

In my case everything works fine. Be sure, you are not trying to use division earlier in your script, because it look's like.
case '/':
$prod = $x / $y;
break;
starts before your function call.

Related

Laravel Throwable not handle exception - get 500 internal error [duplicate]

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.
On PHP7 you can use DivisionByZeroError
try {
echo 1/0;
} catch(DivisionByZeroError $e){
echo "got $e";
} catch(ErrorException $e) {
echo "got $e";
}
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
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();
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()
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.
if(#eval("\$result = $expresion;")===FALSE){
$result=0;
}
Won't just catch divide by 0 errors though.
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;
}
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.
If no additional handling is necessary you could also just do
$division = $foo / ($bar ?: 1);
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
}
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).
Problem:
b=1; c=0;
a=b/c;
// Error Divide by zero
Solution simple:
if(c!=0) a=b/c;
else // error handling
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 ) )");
using intdiv and DivisionByZeroError:
try {
$a = 5;
$b = 0;
intdiv($a,$b);
}
catch(DivisionByZeroError $e){
echo "got {$e->getMessage()}";
}
This is the best way I found to accomplish this:
error_clear_last(); // Clear any previous error
$result = #(1/0); // Executes the division, suppressing the errors
$e = error_get_last(); // Catches the last error
if ($e !== null && $e['message'] == 'Division by zero') {
// Division by zero occurred, do something here
}

PHP Try catch issue [duplicate]

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.
On PHP7 you can use DivisionByZeroError
try {
echo 1/0;
} catch(DivisionByZeroError $e){
echo "got $e";
} catch(ErrorException $e) {
echo "got $e";
}
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
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();
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()
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.
if(#eval("\$result = $expresion;")===FALSE){
$result=0;
}
Won't just catch divide by 0 errors though.
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;
}
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.
If no additional handling is necessary you could also just do
$division = $foo / ($bar ?: 1);
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
}
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).
Problem:
b=1; c=0;
a=b/c;
// Error Divide by zero
Solution simple:
if(c!=0) a=b/c;
else // error handling
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 ) )");
using intdiv and DivisionByZeroError:
try {
$a = 5;
$b = 0;
intdiv($a,$b);
}
catch(DivisionByZeroError $e){
echo "got {$e->getMessage()}";
}
This is the best way I found to accomplish this:
error_clear_last(); // Clear any previous error
$result = #(1/0); // Executes the division, suppressing the errors
$e = error_get_last(); // Catches the last error
if ($e !== null && $e['message'] == 'Division by zero') {
// Division by zero occurred, do something here
}

Error handling for division by zero [duplicate]

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.
On PHP7 you can use DivisionByZeroError
try {
echo 1/0;
} catch(DivisionByZeroError $e){
echo "got $e";
} catch(ErrorException $e) {
echo "got $e";
}
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
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();
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()
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.
if(#eval("\$result = $expresion;")===FALSE){
$result=0;
}
Won't just catch divide by 0 errors though.
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;
}
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.
If no additional handling is necessary you could also just do
$division = $foo / ($bar ?: 1);
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
}
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).
Problem:
b=1; c=0;
a=b/c;
// Error Divide by zero
Solution simple:
if(c!=0) a=b/c;
else // error handling
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 ) )");
using intdiv and DivisionByZeroError:
try {
$a = 5;
$b = 0;
intdiv($a,$b);
}
catch(DivisionByZeroError $e){
echo "got {$e->getMessage()}";
}
This is the best way I found to accomplish this:
error_clear_last(); // Clear any previous error
$result = #(1/0); // Executes the division, suppressing the errors
$e = error_get_last(); // Catches the last error
if ($e !== null && $e['message'] == 'Division by zero') {
// Division by zero occurred, do something here
}

Nested negative if statements with assignments in condition

I have the following piece of code:
if (!$x = some_function(1)) {
if (!$x = some_function(2)) {
return something;
}
}
I want to know which of the following statements are equivalent:
A.
if (some_function(1)) {
$x = some_function(1));
}
else if (some_function(2)) {
$x = some_function(2));
}
else {
return something;
}
Or if it's essentially saying that it should be overridden, like so:
B.
if (some_function(1)) {
$x = some_function(1));
}
if (some_function(2)) {
$x = some_function(2));
}
if (!$x) {
return something;
}
Another way of wording the question: in an assignment within an if statement, is the variable evaluated for false first, and then assigned if false, or does the assignment happen first, and then the variable evaluated next?
The first statement is not equivalent to any of the others. It would be equivalent to this:
$x = some_function(1); // assign $x first
if(!$x){ // check if $x is falsy
$x = some_function(2); // overwrite $x (not the function itself)
if(!$x){ // check if $x is still falsy
// do stuff
}
}
Or, if the variable is not important, this is also equivalent
if(!some_function(1) && !some_function(2)){...}
the only difference is the first one always provides a value to $x, which is probably used somewhere else.
This is also the same, using ternary
$x = some_function(1) ? some_function(1) : some_function(2);
if(!$x) // do stuff
Thanks Scuzzy for the clarification -- it seems like the proper equivalent is this:
if (some_function(1)) {
$x = some_function(1));
}
if (!$x && some_function(2)) {
$x = some_function(2));
}
if (!$x) {
return something;
}

PHP, How to catch a division by zero?

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.
On PHP7 you can use DivisionByZeroError
try {
echo 1/0;
} catch(DivisionByZeroError $e){
echo "got $e";
} catch(ErrorException $e) {
echo "got $e";
}
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
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();
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()
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.
if(#eval("\$result = $expresion;")===FALSE){
$result=0;
}
Won't just catch divide by 0 errors though.
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;
}
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.
If no additional handling is necessary you could also just do
$division = $foo / ($bar ?: 1);
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
}
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).
Problem:
b=1; c=0;
a=b/c;
// Error Divide by zero
Solution simple:
if(c!=0) a=b/c;
else // error handling
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 ) )");
using intdiv and DivisionByZeroError:
try {
$a = 5;
$b = 0;
intdiv($a,$b);
}
catch(DivisionByZeroError $e){
echo "got {$e->getMessage()}";
}
This is the best way I found to accomplish this:
error_clear_last(); // Clear any previous error
$result = #(1/0); // Executes the division, suppressing the errors
$e = error_get_last(); // Catches the last error
if ($e !== null && $e['message'] == 'Division by zero') {
// Division by zero occurred, do something here
}

Categories