note 129594 added to language.types.integer
| From: | lewismoten@gmail.com | Date: | Tue, 18 Jun 2024 08:59:16 +0000 |
| Subject: | note 129594 added to language.types.integer | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221382@lists.php.net to get a copy of this message | ||
Regarding the part about
PHP does not support unsigned ints, this often causes much
confusion when using the hard-coded minimum value of a signed integer that matches PHP_INT_MIN.
<?php
// 64-bit example
var_dump(PHP_INT_MIN);
var_dump(-9223372036854775808);
var_dump(PHP_INT_MIN === -9223372036854775808);
// int(-9223372036854775808)
// float(-9.223372036854776E+18)
// bool(false)
?>
Although visually, I've typed the same value that PHP_INT_MIN writes out
-9223372036854775808, the language parser only understands it as two expressions with a
negate operator followed by 9223372036854775808. The value exceeds the maximum value of
an integer by one, and is promoted to a float. Although it's been suggested in the past to wire
up a hook to look for this value specifically, it's more difficult than it sounds. The
tokenizer is unable to evaluate both the negate and integer as one token. In addition, you would
also need to address binary, octal, and hex literals.
<?php
var_dump(-9223372036854775808); // literal decimal
var_dump(-0x8000000000000000); // literal hex
var_dump(-0b1000000000000000000000000000000000000000000000000000000000000000); // literal binary
var_dump(-01000000000000000000000); // literal octal
?>
If you need to hard-code the minimum value, use PHP_INT_MIN. It was introduced
specifically for this edge case. Alternative methods are to write -9223372036854775807 -
1.
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 204.111.141.3)
----
Manual Page -- https://php.net/manual/en/language.types.integer.php
Edit -- https://main.php.net/note/edit/129594
Del: integrated -- https://main.php.net/note/delete/129594/integrated
Del: useless -- https://main.php.net/note/delete/129594/useless
Del: bad code -- https://main.php.net/note/delete/129594/bad+code
Del: spam -- https://main.php.net/note/delete/129594/spam
Del: non-english -- https://main.php.net/note/delete/129594/non-english
Del: in docs -- https://main.php.net/note/delete/129594/in+docs
Del: other reasons-- https://main.php.net/note/delete/129594
Reject -- https://main.php.net/note/reject/129594
Search -- https://main.php.net/manage/user-notes.php