PHP 8.4: round() - Invalid rounding modes throw \ValueError exceptions


The round() function rounds a float value to the nearest integer or a decimal value of a specified precision. It supports fine-tuning the rounding method with an additional parameter.

Prior to PHP 8.4, passing an invalid rounding mode parameter silently assumed the default PHP_ROUND_HALF_UP rounding mode. In PHP 8.4 and later, passing invalid rounding mode results in a \ValueError exception, and is no longer assumed as PHP_ROUND_HALF_UP.

round(num: 3.14, mode: 42); // Invalid $mode parameter
ValueError: round(): Argument #3 ($mode) must be a valid rounding mode (PHP_ROUND_*).

Related Changes

Backward Compatibility Impact

Passing an invalid $mode parameter to the round() function results in a ValueError exception being thrown in PHP 8.4 and later. This is intended to correct the possibly unexpected behavior in previous PHP versions that assumed the default PHP_ROUND_HALF_UP rounding mode upon invalid values.

PHP code that do not pass a $mode parameter to the round function calls at all, or pass correct parameter values (PHP_ROUND_* constants) will not see any functionality changes. Any PHP applications that receive the exceptions can safely drop the additional parameter to mimic the existing behavior without causing the exception.

- round(num: 3.14, mode: 42);
+ round(num: 3.14);