首页 文章资讯内容详情

PHP ArithmeticError

2026-06-04 1 花语

介绍

ArithmeticError类是从Error类继承的。在执行某些数学运算时可能会发生此类错误。一种这样的情况是尝试执行负量的按位移位操作。当调用 intdiv()函数导致值超出整数的合法范围时,也会引发此错误。

ArithmeticError示例

在下面的示例中,尝试将二进制移位运算符与负操作数一起使用。这导致ArithmeticError。

示例

<?php try { $a = 10; $b = -3; $result = $a << $b; } catch (ArithmeticError $e) { echo $e->getMessage(); } ?>

输出结果

这将产生以下结果-

Bit shift by negative number

如果对 intdiv()函数的调用导致无效的整数,则会引发ArithmeticError。如下例所示,PHP(PHP_INT_MIN)中允许的最小整数不能除以-1

示例

<?php try { $a = PHP_INT_MIN; $b = -1; $result = intdiv($a, $b); echo $result; } catch (ArithmeticError $e) { echo $e->getMessage(); } ?>

输出结果

这将产生以下结果-

Division of PHP_INT_MIN by -1 is not an integer