Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - How to suppress the "Division by zero" error and set the result to null for the whole application?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...