php / php-src

The PHP Interpreter
https://www.php.net
Other
37.84k stars 7.72k forks source link

is_float(integer) == true #14043

Closed SailorMax closed 4 months ago

SailorMax commented 4 months ago

Description

The following code:

<?php
var_dump( is_float(12300000000000000000) );

Resulted in this output:

bool(true)

But I expected this output instead:

bool(false)

PHP Version

PHP 8.3.3

Operating System

Debian 11

nielsdos commented 4 months ago

That's because it's not an integer, but a float: https://3v4l.org/Ynacn When you type a number literal so large it can't fit in an integer, you get a float literal instead of an int literal.

SailorMax commented 4 months ago

I understand the reason, but this is bad solution with unexpected results. If we will try to write some kind of specific encoder (messagepack for example) we will get broken result.

nielsdos commented 4 months ago

The automatic change from integer to float happens in many cases, e.g. also when doing some arithmetic e.g. PHP_INT_MAX+1. If you want to change such a fundamental behaviour you'll have to contact the mailing list and do an RFC.