PHP ArithmeticError
phpserver side programmingprogramming更新于 2025/7/18 12:07:17
简介
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

