Implicit conversion from float %.*H to int loses precision

Description

This warning is emitted when a float is passed to a typed value that requires an integer. The float is truncated, not rounded, to an integer, and passed.

This becomes a Fatal error when strict_types is actived.

The float might be a valid integer, when it has no decimal part: then, no error is emitted.

Example

<?php

function foo(int $a): int {
    // At return time
    return $a - 0.5;
}

// At call time
foo(1.1);

?>

Literal Examples

  • Implicit conversion from float 1.1 to int loses precision

  • Implicit conversion from float 0.5 to int loses precision

Solutions

  • Cast the float to integer before using it with a typed resource.

  • Round the float to integer before using it with a typed resource.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See implicitConversionToInt.