PHP Number Systems - Understanding Octal Representation

PHP supports multiple number systems through specific notation conventions. The octal (base-8) system uses a leading zero prefix for literal values.

<?php echo 01234;

Output Analysis

This code outputs 668 (decimal). PHP interprets numeric literals beginning with 0 as octal values, converting 1234 (base-8) to 668 (base-10). The PHP documentation provides detailed information about integer notation.

Octal Number System

The octal system uses eight digits (0 through 7). Each position represents a power of 8.

Conversion Process

Converting octal to decimal involves multiplying each digit by the corresponding power of 8:

1×8³ + 2×8² + 3×8¹ + 4×8⁰ = 66810

The first thing first, so if you have look in the expression above is simple to deduce that we are splitting the numbers and then applying times 8. So the right question is why are we using ³, ², ¹ and ⁰? It comes from { 0, 1, 2, 3, 4, 5, 6, 7 }, do you remember that? So for each number in the expression you’ll use these numbers from right to left.

1×8³ + 2×8² + 3×8¹ + 4×8 = 66810

Breaking down into peaces and then stick together again

1×8³ = 512 2×8² = 128 3×8¹ = 24 4×8⁰ = 4 512 + 128 + 24 + 4 = 66810

References

[1] PHP documentation - Language types