Implicit Array Key Conversion¶
Array keys accept only string and integer types. When providing a float, PHP used to convert it to an int. It still does, in PHP 8.1, though it now emits a deprecation warning.
PHP code¶
<?php
$a = [];
$a[15.5] = 2; // deprecated, as key value loses the 0.5 component
$a[15.0] = 3; // ok, as 15.0 == 15
print $a[15];
?>
Before¶
2
After¶
PHP Deprecated: Implicit conversion from float 15.5 to int loses precision in /codes/implicitConversionToInt.php on line 3
Deprecated: Implicit conversion from float 15.5 to int loses precision in /codes/implicitConversionToInt.php on line 3
3
PHP version change¶
This behavior was deprecated in 8.1
This behavior changed in 9.0