Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP 8.1+ warns that a float lost its decimals while becoming an int

ErrorDeprecated: Implicit conversion from float 8.1 to int loses precision

Short answer

Since PHP 8.1, PHP warns when a float with a fractional part is turned into an int without you asking. The message names the float. Find where that value is used as an array key, an int parameter or return value, or the result of a division, then convert it on purpose with a cast, round() or intdiv().

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Make PHP show the file and line, then read the float named in the message. The number tells you which calculation produced it.

    php -d display_errors=1 -d error_reporting=E_ALL index.php
  2. 2Match what you see, then jump to its fix

    1. 1The line contains $items[$x] or $items[$a / $b] and the float in the message has a decimal part such as 15.5A fractional float is used as an array keyFix
    2. 2The line is a function call or a return statement, and the function declares int in its signatureA float is passed to an int parameter or returned from an int functionFix
    3. 3The float in the message is a long fraction like 3.5714285714286The result of a division ends up where an int is neededFix

What does the error look like?

  • Deprecated: Implicit conversion from float 8.1 to int loses precision
  • Deprecated: Implicit conversion from float 15.5 to int loses precision in /var/www/app/index.php on line 12

Cause 1 of 3

A fractional float is used as an array key

1Check

Print the key before it is used. A key like 15.0 is fine because nothing is lost; 15.5 loses the 0.5 and triggers the notice.

var_dump($key);
var_dump($items[$key]);

2Fix

Decide what the key should be and convert it yourself: truncate with an (int) cast, or choose the rounding rule with round().

$items[(int) $key];
$items[(int) round($key)];

3Verify

Run the script again with all errors on; the Deprecated line for that file and line is gone.

php -d display_errors=1 -d error_reporting=E_ALL index.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A float is passed to an int parameter or returned from an int function

1Check

Look at the declared type. In the default coercive mode PHP used to cut the decimals silently; now it warns when anything would be lost. Whole-number floats such as 8.0 still pass.

function foo($value): int { return $value; }
var_dump(foo(8.1)); // Deprecated
var_dump(foo(8.0)); // int(8), no warning

2Fix

Convert at the call site or inside the function with an explicit cast. Casting rounds towards zero, so use round() first if you want the nearest whole number.

function foo($value): int { return (int) round($value); }

3Verify

Call the function with a fractional value and confirm no Deprecated line is printed.

php -d error_reporting=E_ALL -r 'function foo($v): int { return (int) round($v); } var_dump(foo(8.1));'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The result of a division ends up where an int is needed

1Check

PHP has no integer division operator: / returns a float whenever the numbers do not divide evenly, and that float then flows into a key, a parameter or an int operator.

php -r 'var_dump(25 / 7);'

2Fix

If you want the whole-number quotient, use intdiv(). If you want rounding, call round() and cast. intdiv() truncates towards zero and throws DivisionByZeroError when the divisor is 0.

$pages = intdiv($total, $perPage);        // 25, 7 gives 3
$pages = (int) ceil($total / $perPage);   // round up instead

3Verify

Check the value you now get for a case that used to be fractional.

php -r 'var_dump(intdiv(25, 7), (int) ceil(25 / 7));'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run tests and local development with error_reporting set to E_ALL so deprecations show up before an upgrade.
  • Convert money, page and offset calculations on purpose with intdiv(), round(), floor() or ceil() instead of relying on automatic conversion.
  • Do not cast a float built from decimals straight to int when the exact result matters: (int) ((0.1 + 0.7) * 10) gives 7, so round first.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.