Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@mageekguy mageekguy commented Apr 7, 2017

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

@remicollet

This comment has been minimized.

Copy link

@remicollet 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

This comment has been minimized.

Copy link

@remicollet remicollet commented Apr 7, 2017

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