如何在整个应用程序中禁止“除零除”错误并将结果设置为空?

如何在整个应用程序中禁止“除零除”错误并将结果设置为空? 通过说“为整个应用程序”,我的意思是它不是一个单一的表达。 相反,每当发生“除零除错”错误时,结果将自动设置为空,并且不会抛出错误。


这应该做的伎俩。

$a = @(1/0); 
if(false === $a) {
  $a = null;
}
var_dump($a);

输出

NULL

请参阅此处的参考错误控制。

编辑

function division($a, $b) {
    $c = @(a/b); 
    if($b === 0) {
      $c = null;
    }
    return $c;
}

在任何地方用函数调用division(1,0)代替1/0

编辑 - 没有第三个变量

function division($a, $b) {         
    if($b === 0)
      return null;

    return $a/$b;
}

简单如..好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
} 

作为一项功能

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

像这样 使用

$number = mydivide(20,5)//equals four

我想不出一种方法来设置它,只要有分歧,但我会使用该函数并将其重命名“d”之类的东西,所以它很短!


这是一个可怕的解决方案,但是,谢天谢地,你不会使用它,因为变量设置为false而不是null

function ignore_divide_by_zero($errno, $errstring)
{
  return ($errstring == 'Division by zero');
}

set_error_handler('ignore_divide_by_zero', E_WARNING);

在你的情况下,我会创建一个功能,为你做分工。

链接地址: http://www.djcxy.com/p/11899.html

上一篇: How to suppress the "Division by zero" error and set the result to null for the whole application?

下一篇: Division by zero error in WordPress Theme