PHP 8.1+ warns that a float lost its decimals while becoming an int
Deprecated: Implicit conversion from float 8.1 to int loses precisionShort 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().
Fix it fast
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.php2Match what you see, then jump to its fix
- 1
The 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
The 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
The float in the message is a long fraction like 3.5714285714286The result of a division ends up where an int is neededFix
- 1
What does the error look like?
Deprecated: Implicit conversion from float 8.1 to int loses precisionDeprecated: 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.phpConfirmed by php.net
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 warning2Fix
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
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 instead3Verify
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix