PHP int Keyword
last modified November 25, 2026
The PHP int keyword represents the integer data type. Integers are whole numbers without a decimal point and may be positive, negative, or zero. This article shows how integers are written, converted, declared, and used in calculations.
The type is scalar and platform-dependent: it is 32 bits on most 32-bit builds and 64 bits on 64-bit builds. PHP does not have an unsigned integer type, and an overflowing result silently becomes a float.
Basic definitions
An integer is a whole number, positive, negative, or zero, written without a decimal point. In PHP the type is called int. The word integer is accepted by casts and by functions such as is_integer, but in a type declaration it is interpreted as a class name; write int there.
Integer literals can be written in decimal, hexadecimal (0x), octal (0 or, since PHP 8.1, 0o), and binary (0b) notation. Since PHP 7.4 an underscore may separate groups of digits, so 1_000_000 is the same value as 1000000.
The size of an integer depends on the platform: 32 bits on most 32-bit systems and 64 bits on 64-bit systems. The constants PHP_INT_SIZE, PHP_INT_MAX, and PHP_INT_MIN describe the current platform. PHP has no unsigned integer type.
An operation that produces a value outside the integer range does not wrap around; the result becomes a float. This is why PHP_INT_MAX + 1 is a floating-point number.
Declaring integers
The first example declares three integer variables and prints them. Large values may be grouped with underscores for readability.
<?php declare(strict_types=1); $age = 25; $temperature = -10; $population = 8_000_000; echo "Age: $age\n"; echo "Temperature: $temperature °C\n"; echo "Population: $population\n"; echo "Type: ", get_debug_type($age), "\n";
The get_debug_type function returns the type of a value as a short string. It is available since PHP 8.0 and is preferable to gettype in error messages and debugging output.
$ php ints.php Age: 25 Temperature: -10 °C Population: 8000000 Type: int
Integer notations
PHP converts every notation to the same internal integer. The hexadecimal literal 0x2A, the octal literals 052 and 0o52, and the binary literal 0b101010 all represent 42.
<?php
declare(strict_types=1);
$decimal = 42;
$hexadecimal = 0x2A;
$octal = 052;
$explicitOctal = 0o52;
$binary = 0b101010;
$million = 1_000_000;
printf("decimal %d\n", $decimal);
printf("hexadecimal %d\n", $hexadecimal);
printf("octal %d\n", $octal);
printf("octal (0o) %d\n", $explicitOctal);
printf("binary %d\n", $binary);
printf("million %d\n", $million);
Leading zeros matter: 052 is octal, while 52 is decimal. The explicit 0o prefix, added in PHP 8.1, makes the intention clearer and avoids the trap of a decimal literal that accidentally starts with zero. Use printf or dechex when a value has to be displayed in another base.
$ php notations.php decimal 42 hexadecimal 42 octal 42 octal (0o) 42 binary 42 million 1000000
Casting to int
There are two common ways to convert a value to an integer: the (int) cast and the intval function. Casting a float truncates the fractional part toward zero; it does not round.
<?php
declare(strict_types=1);
$values = [3.99, -3.99, "123", "12 apples", "abc", true, false, null];
foreach ($values as $value) {
echo get_debug_type($value), " => ", (int) $value, "\n";
}
echo "intval base 16: ", intval("1A", 16), "\n";
echo "intval base 2: ", intval("101010", 2), "\n";
A numeric string is read up to the first character that is not part of a number, so "12 apples" becomes 12; a string with no leading digits becomes 0. true becomes 1 and false and null become 0. The second argument of intval selects the base, which is why intval("1A", 16) is 26.
Casting is not validation. The string "abc" silently becomes 0, which may look like a valid value. When the input comes from a user or a file, validate it first, for example with filter_var($value, FILTER_VALIDATE_INT) or ctype_digit.
$ php casting.php float => 3 float => -3 string => 123 string => 12 string => 0 bool => 1 bool => 0 null => 0 intval base 16: 26 intval base 2: 42
Integer type declarations
The int type can be used for parameters and return values. With declare(strict_types=1), PHP checks the declared types without implicit scalar conversion.
<?php
declare(strict_types=1);
function addNumbers(int $a, int $b): int
{
return $a + $b;
}
function percentage(int $part, int $total): float
{
return $part / $total * 100;
}
echo addNumbers(5, 7), "\n";
echo percentage(3, 4), "\n";
The addNumbers function returns an int, while percentage returns a float because division with the / operator always produces a float. Declaring the return type documents the result and lets PHP check it at runtime.
$ php declarations.php 12 75
Strict and weak typing
Without strict_types, PHP converts scalar arguments to the declared type when the conversion is safe. A separate file is needed because declare(strict_types=1) applies to the whole file in which it appears.
<?php
// no strict_types in this file
function addNumbers(int $a, int $b): int
{
return $a + $b;
}
echo addNumbers("5", "7"), "\n";
In weak mode the strings "5" and "7" are converted to integers. In a file with strict types, the same call raises a TypeError instead. Strict mode is recommended for new code: a string that happens to contain digits should usually be validated and cast explicitly rather than accepted silently.
$ php coercion.php 12
Arithmetic and division
The usual arithmetic operators work with integers. The / operator always returns a float, even when the division is exact; use intdiv when an integer result is required.
<?php declare(strict_types=1); $a = 10; $b = 3; echo "a + b: ", $a + $b, "\n"; echo "a - b: ", $a - $b, "\n"; echo "a * b: ", $a * $b, "\n"; echo "a / b: ", $a / $b, "\n"; echo "intdiv: ", intdiv($a, $b), "\n"; echo "a % b: ", $a % $b, "\n"; echo "a ** b: ", $a ** $b, "\n";
intdiv(10, 3) truncates toward zero. The modulus operator % returns the remainder and takes the sign of the dividend, so -7 % 3 is -1. Exponentiation with ** can overflow to a float even when both operands are integers. Division by zero with intdiv raises a DivisionByZeroError, and % raises the same error.
$ php operations.php a + b: 13 a - b: 7 a * b: 30 a / b: 3.3333333333333 intdiv: 3 a % b: 1 a ** b: 1000
Limits and overflow
The constants PHP_INT_MAX and PHP_INT_MIN hold the largest and smallest integers supported by the platform. Exceeding the range converts the result to a float.
<?php declare(strict_types=1); echo "PHP_INT_SIZE: ", PHP_INT_SIZE, " bytes\n"; echo "PHP_INT_MAX: ", PHP_INT_MAX, "\n"; echo "PHP_INT_MIN: ", PHP_INT_MIN, "\n"; $max = PHP_INT_MAX; $overflow = $max + 1; var_dump($max); var_dump($overflow); var_dump(is_int($overflow));
The sum is no longer an integer, so is_int returns false and arithmetic on it loses precision beyond the 53 bits a float can represent exactly. For large integer arithmetic use the bcmath or gmp extension, or a library such as Brick/Math.
$ php limits.php PHP_INT_SIZE: 8 bytes PHP_INT_MAX: 9223372036854775807 PHP_INT_MIN: -9223372036854775808 int(9223372036854775807) float(9.223372036854776E+18) bool(false)
Useful integer functions
PHP provides many functions for inspecting and formatting integers. The example below also shows how to validate an integer string and how to generate a reproducible pseudo-random number for a demonstration.
<?php
declare(strict_types=1);
$number = -42;
echo "abs: ", abs($number), "\n";
echo "is_int: ", var_export(is_int($number), true), "\n";
echo "max: ", max(4, 9, 2), "\n";
echo "min: ", min(4, 9, 2), "\n";
echo "formatted: ", number_format(1234567.891, 2, '.', ','), "\n";
echo "validated: ", var_export(filter_var('42', FILTER_VALIDATE_INT), true), "\n";
echo "rejected: ", var_export(filter_var('4.2', FILTER_VALIDATE_INT), true), "\n";
mt_srand(2026);
echo "mt_rand: ", mt_rand(1, 100), "\n";
filter_var with FILTER_VALIDATE_INT returns the integer or false; it rejects "4.2" instead of truncating it. mt_srand makes mt_rand reproducible, which is useful in tests, but neither function is suitable for passwords or tokens. Use random_int for values that must be unpredictable, and number_format only for display, never for storage.
$ php functions.php abs: 42 is_int: true max: 9 min: 2 formatted: 1,234,567.89 validated: 42 rejected: false mt_rand: 6
Best practices
Use the following guidelines when working with integers.
- Type safety: declare
intparameters and return values and enablestrict_types=1. - Validation: validate external input with
filter_varinstead of relying on a cast. - Division: use
intdivwhen the result must be an integer, and check the divisor first. - Overflow: remember that the result of an overflowing operation becomes a float; use BCMath or GMP when the range matters.
- Readability: group long literals with underscores and avoid octal literals unless the base is intentional.
- Randomness: use
random_intfor security-sensitive values.
Source
PHP manual: integers PHP manual: intdiv PHP manual: filter_var PHP manual: reserved constants
In this article we have worked with the PHP int type: its
literals, casts, declarations, arithmetic, limits, and helper functions. See
also the PHP float,
bool, and
union types tutorials.
Author
List all PHP tutorials.