1.2.468. Float Conversion As Index

Only integers and strings are possible types when used for array index. Until PHP 8.1, floats were converted to integer by truncation. Since PHP 8.1, a deprecated message is emitted.

The implicit conversion of float to int which leads to a loss in precision is now deprecated. This affects array keys, int type declarations in coercive mode, and operators working on integers.

<?php
$a = [];
$a[15.5]; // deprecated, as key value loses the 0.5 component
$a[15.0]; // ok, as 15.0 == 15
?>

See also Implicit incompatible float to int conversions.

1.2.468.2. Connex PHP features

1.2.468.2.1. Suggestions

  • Use the cast operator (string) to convert the floats into strings. The reverse cast is most often possible.

1.2.468.2.2. Specs

Short name

Arrays/FloatConversionAsIndex

Rulesets

All, Analyze, Changed Behavior, CompatibilityPHP81, CompatibilityPHP82

Exakat since

2.3.1

PHP Version

With PHP 8.1 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Available in

Entreprise Edition, Exakat Cloud