1

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?

3
  • Add some more info Commented May 12, 2017 at 17:39
  • Ok echo PHP_INT_MIN === -9223372036854775808; is false. Nonetheless, echo PHP_INT_MIN; displays -9223372036854775808 and echo is_int(-9223372036854775808); is false Commented May 12, 2017 at 17:39
  • Sorry I'm wondering why the value displayed by PHP_INT_MIN does not appear to be an integer when checking with is_int() Commented May 12, 2017 at 17:40

3 Answers 3

3

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)
*/
Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

Thanks for pointing this out. Would have accepted this as an answer had I not done so already
The comment you cite from the PHP.net comments looks invalid in PHP7 to me, as integers can be signed. (See my answer)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.