Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Last active April 7, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageekguy/d71e784ef046e0342d52cc5d5b6d8bd2 to your computer and use it in GitHub Desktop.
Save mageekguy/d71e784ef046e0342d52cc5d5b6d8bd2 to your computer and use it in GitHub Desktop.
About PHP_INT_MAX
# php --version
PHP 7.0.12 (cli) (built: Nov 3 2016 12:54:26) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
# php -a
php > var_dump((PHP_INT_MAX + 1) > PHP_INT_MAX);
bool(false)
php > var_dump((PHP_INT_MAX + 1) < PHP_INT_MAX);
bool(false)
php > var_dump((PHP_INT_MAX + 1) == PHP_INT_MAX);
bool(true)
php > var_dump((PHP_INT_MAX + 1000) == PHP_INT_MAX);
bool(true)
@mageekguy
Copy link
Author

For the record, (string) (PHP_INT_MAX + 1) > PHP_INT_MAX works like a charm.

@remicollet
Copy link

remicollet commented Apr 7, 2017

Both are compared as float, so less significant digits are lost (63 bits for integer, 52 bits for float)

$ php -r 'var_dump(gmp_init(PHP_INT_MAX) + gmp_init(1) > gmp_init(PHP_INT_MAX));'
bool(true)

@remicollet
Copy link

IIRC there is an old proposal to switch to gmp by default on interger overflow.
BTW, huge performance issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment