How to suppress the "Division by zero" error and set the result to null for the whole application? By saying "for the whole application", I mean it is not for a single expression. Instead, whenever a "Division by zero" error occurs, the result is set to null automatically and no error will be thrown.
Asked
Active
Viewed 4.4k times
12
-
If you're looking for c++ operator overloading possibility, so the php doesn't support it, see the discussion here: http://stackoverflow.com/questions/787692/operator-overloading-in-php – Igor Sep 17 '10 at 00:27
-
Suppressing errors is generally considered a bad practice. You can use `try` and `catch`, but not `@`. You should write your code in such way that incorrect values are sanitized and no errors or warnings are thrown by standard execution of your code. – Mike Sep 26 '13 at 14:53
-
in `SQL Server`, there is a little trick `NULLIF()` http://www.bennadel.com/blog/984-using-nullif-to-prevent-divide-by-zero-errors-in-sql.htm – Jaider Apr 29 '14 at 20:24
-
You could use a custom exception/error handler to catch it and set the result to 0 ... PHP 7 provides a 'DivisionByZeroError' exception class http://php.net/manual/en/class.divisionbyzeroerror.php ... in previous versions, it may be possible to convert the corresponding error to an exception and then set a handler. – Aaron Wallentine Oct 24 '17 at 22:21
4 Answers
22
This should do the trick.
$a = @(1/0);
if(false === $a) {
$a = null;
}
var_dump($a);
outputs
NULL
See the refs here error controls.
EDIT
function division($a, $b) {
$c = @(a/b);
if($b === 0) {
$c = null;
}
return $c;
}
In any place substitute 1/0
by the function call division(1,0)
.
EDIT - Without third variable
function division($a, $b) {
if($b === 0)
return null;
return $a/$b;
}
-
Wow thats so simple (I wish I thought of it) but does it set it to null? – Mark Lalor Sep 16 '10 at 23:57
-
2This is the best general concept. However, it fails when `a == 0`. You should change the conditional to be `if ($c === false)`. But actually, you should just check if `$b == 0`. ;) – Matthew Sep 17 '10 at 00:56
-
Even though this is not exactly what I want, there might be not better solution. – Ethan Sep 17 '10 at 03:08
-
1You missed the $ sign function division(a,b) must be division($a,$b) – Adrian P. Nov 21 '13 at 15:02
-
One-liner procedural: `$v = ( @($a/$b) ) ?: null;`. One-liner functional: `return ( $b === 0 ) ? null : $a/$b;` – Andrew Tibbetts Apr 28 '16 at 13:58
5
Simple as.. well abc*123-pi
$number = 23;
$div = 0;
//If it's not 0 then divide
if($div != 0)
$result = $number/$div;//is set to number divided by x
}
//if it is zero than set it to null
else{
$result = null;//is set to null
}
As a function
function mydivide($divisior, $div){
if($div != 0)
$result = $divisor/$div;//is set to number divided by x
}
//if it is zero than set it to null
else{
$result = null;//is set to null
}
return $result;
}
Use it like this
$number = mydivide(20,5)//equals four
I can't think of a way to set it whenever there's division but I'd use the function and rename it to something like "d" so it's short!

Mark Lalor
- 7,820
- 18
- 67
- 106
5
This is a horrible solution, but thankfully, you won't use it because the variable is set to false
instead of null
.
function ignore_divide_by_zero($errno, $errstring)
{
return ($errstring == 'Division by zero');
}
set_error_handler('ignore_divide_by_zero', E_WARNING);
In your case, I'd create a function that does your division for you.

Matthew
- 47,584
- 11
- 86
- 98
2
What about using a ternary operator, like so:
$a = $c ? $b/$c : null;

Nathaniel Ford
- 20,545
- 20
- 91
- 102

ToMSp
- 29
- 2