Skip to content
Tutorials Center
My learning

Topics

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).

PHP
$a = 1_000_000;   // underscores for readability
$b = 0xFF;        // 255
$c = PHP_INT_MAX + 1;   // float, not int

Try it

PHP Literals, limits and overflow

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.

PHP Converting to int

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

PHP
$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 + 1 is the float 9.223372036854776E+18. Test with is_int() or use intdiv() and bcmath/gmp for exact big numbers.
  • / returns an int only when the division is exact. 8 / 2 is int(4), 7 / 2 is float(3.5). Use intdiv() for truncated integer division. Division by zero throws DivisionByZeroError; % by zero too.
  • % takes the sign of the left operand: -7 % 3 is -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" gives 42 with a warning when used in arithmetic, while a completely non-numeric string throws TypeError in arithmetic. (int) "abc" quietly gives 0.
  • Octal trap: 017 is 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 key 5. See array keys.
  • 32-bit builds have PHP_INT_MAX of 2147483647. Write code that does not depend on 64 bits when it matters.