Reputation: 1136
Hi I'm wondering for the following output:
<?php
echo 0500; // Output: 320
echo "<br>";
echo 500; //Output: 500
?>
Any little explanation about this?
Upvotes: 2
Views: 947
Reputation: 4021
PHP renders any integer starting with a 0
(zero) as an octal (base 8) integer. If you echo
it, however, you will get its decimal value. The page for Integers in the PHP manual says:
Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).
Binary integer literals are available since PHP 5.4.0.
To use octal notation, precede the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.
UPDATE: It may or may not be a sweet formula, but Wikipedia recommends this one for the octal to decimal conversion:
In this formula, ai is an individual octal digit being converted, where i is the position of the digit (counting from 0 for the right-most digit).
Let’s convert 37368 to decimal:
37368 = (3×83)+(7×82)+(3×81)+(6×80) = 1536+448+24+6 = 201410
Upvotes: 7