In this PHP tutorial, you shall learn about the Integer datatype, the representation of integer in different formats, and how to use integer values in PHP programs, with the help of examples.
PHP Integer
PHP Integer values can be an integer number in the range of {PHP_INT_MIN, …, -2, -1, 0, 1, 2, …, PHP_INT_MAX}.
The size of an integer is platform dependent. You can get the minimum and maximum integer values using the constants PHP_INT_MIN and PHP_INT_MAX.
Integer Formats
In PHP, Integers can be represented in the following formats.
- Decimal
- Octal
- Hexadecimal
- Binary
Integer in decimal format
Decimal format of integer should always start with a digit in the range [1, 9] followed by the digits in range [0, 9]. There is an exception that the complete number can be zero.
<?php $a = 25; $b = 0; ?>
Integer in octal format
Octal format of an integer should start with 0
followed by the octal number.
<?php $a = 031; ?>
Integer in hexadecimal format
Hexadecimal format of an integer should start with either 0x
or 0X
followed by the hexadecimal number.
<?php $a = 0x19; $b = 0X19; ?>
Integer in binary format
Binary format of an integer should start with either 0b
or 0B
followed by the binary number in ones and zeros.
<?php $a = 0b00011001; ?>
PHP_INT_MIN and PHP_INT_MAX constants
As the minimum and maximum values of an integer change with platform you are working on, you can programmatically get these values using the constants PHP_INT_MIN and PHP_INT_MAX.
PHP Program
<?php $min = PHP_INT_MIN; $max = PHP_INT_MAX; echo($min. '<br>'); echo($max. '<br>'); ?>
Output
Conclusion
In this PHP Tutorial, we learned about Integers in PHP, different formats in which Integers can be represented, minimum and maximum values of an Integer.