Skip to content

"Casting out of range floats to int" applies to strings #20745

@bwoebi

Description

@bwoebi

Description

The following code:

<?php var_dump((int) "9999999999999999999");

Resulted in this output:

PHP Warning:  The float-string "9999999999999999999" is not representable as an int, cast occurred in Command line code on line 1
int(9223372036854775807)

But I expected this output instead:

int(9223372036854775807)

I just updated to PHP 8.5.0 and surprisingly started seeing these warnings.

This was added with https://wiki.php.net/rfc/warnings-php-8-5#casting_out_of_range_floats_to_int, which explicitly talked about warning on non-representable floats. Not about strings. (I see the UPGADING entry got the or strings that look like floats added. But the RFC, what was voted on, does not.)

Now, e.g. (int) "a" is fine emitting 0 as expected ... but once that string is something looking like a number, it suddenly emits a warning.
This is especially problematic when accepting arbitrary user input like (int) ($_POST['value'] ?? 0).

I strongly object to this behaviour on strings - on floats, it's fine (and was voted on).

PHP Version

PHP 8.5.0

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions