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.
Thursday, 21 December 2017
php - How to suppress the "Division by zero" error and set the result to null for the whole application?
itemprop="text">
class="post-text" itemprop="text"> class="normal">Answer
This
should do the trick.
$a = @(1/0);
if(false === $a) {
$a =
null;
}
var_dump($a);
outputs
NULL
See
the refs here rel="noreferrer">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;
}
Subscribe to:
Post Comments (Atom)
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...
-
I would like to split a String by comma ',' and remove whitespace from the beginning and end of each split. For example, if I have ...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I have a method in repository with this implementation which returns a Task Task > GetAllAppsRequestAsync(); I write the getter which cal...
No comments:
Post a Comment