Datatypes in PHP - Integers : Tutorial




2. Integer data type

Integers can be natural numbers (i.e. 1,2,3 ...), zero or negatives of natural numbers (i.e. -1,-2,-3 ...).

In PHP we can have Integer values as,
1. Decimal (base 10) : preceded with +/- signs (+ is optional)
2. Hexa-Decimal (base 16) : prefixed 0
3. Octal (base 8) : prefixed 0x
4. Binary (base 2) : prefixed 0b

<?php

 $var1 = 12345;   // Positive decimal number
 $var2 = -12345;  // Negative decimal number
 $var3 = 012345;  // Octal number
 $var4 = 0x12C;   // hexadecimal number
 $var5 = 0b1010;  // binary number 

 echo $var1."<br>" ;
 echo $var2."<br>" ;
 echo $var3."<br>" ;
 echo $var4."<br>" ;
 echo $var5."<br>" ;

?>
Output

12345 -12345 5349 300 10
Size of a integer is platform-dependent.
For 32-bit platform its about : 2e9
For 64-bit platform its about : 9e18

Note : If an invalid digit is assigned in an octal integer (i.e. 8 or 9), the rest of the number is ignored.
<?php
 
 $var = 0192345;  // Octal number with invalid 9 as second digit.

 echo $var;
 
?>
Output

1