PHP Reference ยท Types and Type Handling
The PHP int type
Reference for the PHP int type: decimal, hex, octal and binary literals, underscores, 64-bit limits, overflow to float, integer division and conversion rules.
An int is a signed whole number. On a 64-bit build, which is what you will meet almost everywhere, it ranges from -9223372036854775808 to 9223372036854775807. The constants PHP_INT_MIN, PHP_INT_MAX and PHP_INT_SIZE describe the limits on your platform.
PHP never wraps around silently. An operation whose result does not fit becomes a float instead, which loses exactness. That is one reason money and IDs should be handled carefully (store cents as integers, or use strings and bcmath for huge values).
$a = 1_000_000; // underscores for readability $b = 0xFF; // 255 $c = PHP_INT_MAX + 1; // float, not int
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
$decimal = 1234; $negative = -1234; $hex = 0x1A; $octal = 0o17; $legacyOctal = 017; $binary = 0b1010; $readable = 1_234_567;
| Form | Prefix | Digits |
|---|---|---|
| Decimal | none | 0-9, no leading zero |
| Hexadecimal | 0x |
0-9, a-f |
| Octal | 0o (8.1) or 0 |
0-7 |
| Binary | 0b |
0, 1 |
An underscore may separate digits in any form, but not at the start, the end or next to the prefix.
Usage notes
- Overflow becomes float.
PHP_INT_MAX + 1is the float9.223372036854776E+18. Test withis_int()or useintdiv()andbcmath/gmpfor exact big numbers. /returns an int only when the division is exact.8 / 2isint(4),7 / 2isfloat(3.5). Useintdiv()for truncated integer division. Division by zero throwsDivisionByZeroError;%by zero too.%takes the sign of the left operand:-7 % 3is-1.- Casting a float truncates toward zero and gives a meaningless result for
NAN, infinity and out-of-range values. PHP 8.5 emits a warning for them (the second example shows(int) 1e19). Check the range first. - Numeric strings convert; leading-numeric strings warn.
"42abc"gives42with a warning when used in arithmetic, while a completely non-numeric string throwsTypeErrorin arithmetic.(int) "abc"quietly gives0. - Octal trap:
017is 15. Leading zeros in input strings are not octal for(int), but literal source code is. - Implicit float-to-int with a fraction is deprecated (PHP 8.1) when used as an array key or for operators like
%and|. - Integer-keyed arrays: the string key
"5"becomes the int key5. See array keys. - 32-bit builds have
PHP_INT_MAXof 2147483647. Write code that does not depend on 64 bits when it matters.
Related
- float, Arithmetic operators, intdiv(), intval(), Base conversion
- Lessons: Numbers, Data types