Summary: in this tutorial, you’ll learn about the PHP int type that represents integers in PHP.
Introduction to the PHP int type
Integers are whole numbers such as -3, -2, -1, 0, 1, 2, 3… PHP uses the int
type to represent the integers.
The range of integers depends on the platform where PHP runs. Typically, integers has a range from -2,147,438,648 to 2,147,483,647. It’s equivalent to 32 bits signed.
To get the size of the integer, you use the PHP_INT_SIZE
constant. Also, you use the PHP_INT_MIN
and PHP_INT_MAX
constants to get the minimum and maximum integer values.
PHP represents integer literals in decimal, octal, binary, and hexadecimal formats.
Decimal numbers
PHP uses a sequence of digits without leading zeros to represent decimal values. The sequence may begin with a plus or minus sign. If it has no sign, then the integer is positive. For example:
2000
-100
12345
From PHP 7.4, you can use the underscores (_) to group digits in an integer to make it easier to read. For example, instead of using the following number:
1000000
you can use the underscores (_) to group digits like this:
1_000_000
Octal numbers
Octal numbers consist of a leading zero and a sequence of digits from 0 to 7. Like decimal numbers, the octal numbers can have a plus (+) or minus (-) sign. For example:
+010 // decimal 8
Code language: JavaScript (javascript)
Hexadecimal numbers
Hexadecimal numbers consist of a leading 0x
and a sequence of digits (0-9) or letters (A-F). The letters can be lowercase or uppercase. By convention, letters are written in uppercase.
Similar to decimal numbers, hexadecimal numbers can include a sign, either plus (+) or minus(-). For example:
0x10 // decimal 16
0xFF // decimal 255
Code language: JSON / JSON with Comments (json)
Binary numbers
Binary numbers begin with 0b
are followed by a sequence of digits 0 and 1. The binary numbers can include a sign. For example:
0b10 // decimal 2
Code language: JavaScript (javascript)
The is_int() function
The is_int()
built-in function returns true
if a value (or a variable) is an integer. Otherwise, it returns false
. For example:
$amount = 100;
echo is_int($amount);
Code language: PHP (php)
Output:
1
Summary
- Integers are whole numbers such as -1, 0, 1, 2…
- Use the
PHP_INT_SIZE
constant to get the size of the integer - Use the
PHP_INT_MIN
andPHP_INT_MAX
constants to get the minimum and maximum integer values. - Literal integers can be in decimal, octal, hexadecimal, and binary forms.
- Use the
is_int()
function returns true if a value (or variable) is an integer.