Decimal
An integer can be specified in decimal, base 10
echo 10; // 10
echo -1; // -1
echo 0; // 0
echo 1452; // 1452
Octal
We use octal when we need a readable format for bit string in group of 3.
rw-r--r-- = 110100100 (binary) = 644 (octal) # Unix access permision - 0755
READ (1) WRITE (1) EXEC (1) = binary 111 = octal 7
READ (1) WRITE (1) EXEC (0) = binary 110 = octal 6
READ (1) WRITE (0) EXEC (0) = binary 100 = octal 4
97 (decimal) = 141 (octal) # To convert decimal to octal, use modulus
97 % 8 => reminder 1
12 % 8 => reminder 4
1 % 8 => reminder 1
1*8^2 + 4*8^1 + 1*8^0 = 64 + 32 + 1 = 97
Binary
To conver binary to octal, convert first to decimal, then to octal.
echo 0b110100100; // 420 - decimal
echo bindec(110100100); // 420 - decimal
echo decoct(0b110100100); // 644 - octal
echo 0644;
// 6*8^2 + 4*8^1 + 4*8^0
// 384 + 32 + 4
// 420
echo 0b1010111100;
// 700
// 001 010 111 100
// 00 are added to complete group of 3 bits
// 001 010 111 100 (binary) = 1274 (octal)
Hexadecimal
Base 16 notation, identified by its leading 0x.
/**
* Hex numbers are often used to represent colours.
* Orange is represented as #FFA500 (255 red, 165 green, 0 blue).
*/
echo 0x121; // 1*16^2 + 2*16^1 + 1*16^0 = 256 + 32 + 1 = 289
echo 0xFF; // 15*16^1 + 15*16^0 = 240 + 15 = 255
Float
Be aware that the float data type is not always capable of representing numbers.
/**
* Float data type is not always capable of representing numbers.
* The result of this simple expression is stored internally as 7.999999
*/
echo (0.1 + 0.7) * 10; // 8
echo (int) ((0.1 + 0.7) * 10); // 7
Last update: 451 days ago