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

前端 未结 4 1060
小鲜肉
小鲜肉 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:07

    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!

提交回复
热议问题