Web lists-archives.com

Re: [PHP] String conversion to integer




Hi Kebin,


> I'm testing php 7.1 in my project, and I got a strange problem.

>
> $string = '584e82ed00819';
> echo (int)$string;
>
> The above code will output 9223372036854775807 (It's PHP_INT_MAX) in php
> 7.1.
> In php 5.5, the result is: 584.
>
> According to the document:
> http://php.net/manual/en/language.types.string.php#language.types.string.conversion
>
> I guess php 7.1 process the string as 584e82, but 5.5 did not.
> Any differences for string conversion between 7.1 and 5.5 ?

Yes, PHP 7.1 brings the change of string casting now recognising scientific
notation. A brief mention of this is made in the migration guide:
"
Integer operations and conversions on numerical strings now respect scientific
notation. This also includes the (int) cast operation, and the following functions:
intval() (where the base is 10), settype(), decbin(), decoct(), and dechex().
"

For more reasoning on why this change has been made, you can view the Warn
about invalid strings in arithmetic RFC[1], specifically the "Fractional and scientific
notation strings with integer operators" subsection.

Thanks,
Tom

[1]: https://wiki.php.net/rfc/invalid_strings_in_arithmetic