How to suppress the “Division by zero” error and set the result to null for the whole application?

前端 未结 4 1063
小鲜肉
小鲜肉 2020-12-20 13:29

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 n

4条回答
  •  不思量自难忘°
    2020-12-20 14:19

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

提交回复
热议问题