zuggy
zuggy

Reputation: 33

Minimum integer in PHP 7

In interactive mode on PHP 7 (64 bit Ubuntu),

php > echo true;
1
php > echo false; # no output for false
php > echo PHP_INT_MIN == -9223372036854775808;
1
php > echo is_int(PHP_INT_MIN);
1
php > echo is_int(-9223372036854775808);

Why doesn't the last line output 1?

Upvotes: 1

Views: 2089

Answers (3)

Code4R7
Code4R7

Reputation: 2958

First PHP parses your integer value as float, because of an integer overflow. Then it uses is_int() to determine if what PHP has parsed is an integer. See example #3 for 64-bit systems.

Please note that is_int() does work with unsigned integers as well. Using 32-bit PHP 7:

echo PHP_INT_MIN;                    // -2147483648
echo var_dump(is_int(-2147483648));  // bool(false)
echo var_dump(is_int(-2147483647));  // bool(true)

To make sure PHP_INT_MIN is off by one, please try this on your 64-bit PHP 7:

echo var_dump(is_int(-9223372036854775807));  // bool(true)?

If you need more integer precision, you could use the GMP extension.

Upvotes: 2

Riccardo Bonafede
Riccardo Bonafede

Reputation: 606

Because is_int :

perates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.

and so -9223372036854775808 it's smaller than your system word bound

Upvotes: 3

Sammitch
Sammitch

Reputation: 32272

var_dump() is your friend.

var_dump(
    PHP_INT_MIN,
    is_int(PHP_INT_MIN),
    -9223372036854775808,
    is_int(-9223372036854775808)
);

/* Output:
int(-9223372036854775808)
bool(true)
float(-9.2233720368548E+18)
bool(false)
*/

Upvotes: 3

Related Questions