diff --git a/Services/Math/classes/class.ilMathPhpAdapter.php b/Services/Math/classes/class.ilMathPhpAdapter.php index b3a34f344c3b..a4c071a244eb 100644 --- a/Services/Math/classes/class.ilMathPhpAdapter.php +++ b/Services/Math/classes/class.ilMathPhpAdapter.php @@ -49,9 +49,20 @@ public function div($left_operand, $right_operand, $scale = null) throw new ilMathDivisionByZeroException(sprintf("Division of %s by %s not possible!", $left_operand, $right_operand)); } - $res = $this->normalize($left_operand) / $this->normalize($right_operand); + // This ensures the old PHP <= 7.0.x behaviour, see: #27785 / #26361 + try { + $res = $this->normalize($left_operand) / $this->normalize($right_operand); + + $division = $this->applyScale($res, $this->normalize($scale)); + } catch (Throwable $e) { + if (strpos($e->getMessage(), 'A non-numeric value encountered') !== false) { + $division = 0; + } else { + throw $e; + } + } - return $this->applyScale($res, $this->normalize($scale)); + return $division; } /** diff --git a/Services/Math/test/ilMathBaseAdapterTest.php b/Services/Math/test/ilMathBaseAdapterTest.php index 56d59effd788..c31af5314035 100644 --- a/Services/Math/test/ilMathBaseAdapterTest.php +++ b/Services/Math/test/ilMathBaseAdapterTest.php @@ -160,7 +160,8 @@ public function mulData() public function divData() { return [ - ['1', '2', '0.5', self::DEFAULT_SCALE] + ['1', '2', '0.5', self::DEFAULT_SCALE], + ['', '2', '0', self::DEFAULT_SCALE], ]; }