Why Does 010 = 8?

Is this Octal?

<?php

echo 010;

So octal means eight, because an octopus has 8 legs. Right?

So if octal is 8 dont you have:
00000000

Then how does 010 equal 8? Is it shorthand somehow?

because if you do 011 it is 9.

so the place holders are like what…
? - 8 - 1?

The formula is easy to figure out if I do this… But why does it use 3 digits?

/*
010	=	8
011 =	9
012 =	10
020 =	16
030 = 	24
*/

Awesome, that’s totally correct :slight_smile:

It took me a while to work it out too.
It’s a neat party trick too :wink:

Okay that actually makes a lot of sense, for the octal I think I have it in my mind a certain way and almost grasp it but is there a limit to how many 0’s?

00000000 = 8 digits, can that still be octal?
010 = 8, so in the string about is that the same as 00000010, we are just shifting the zero’s… Actually when i write this it seems like it is making sense now…

When doing octal, in PHP you place the ZERO before the actual octal, does that discard the first 0?

011 = 9, you can’t use the zero can you? Or would you do…
0111 = 25 (I think thats 25)

I have been reading on hexidecimal also and figure it out kind of, im not fast at it by all means.

It goes:
1 2 3 4 5 6 7 8 9 A B C D E F

so…
0x1 = 1
0xA = 10
0xF = 15

Then it gets tricky…
0x10 goes to 16 (right after the F, add a 0 after the one),
0x11 goes to 17

then when you hit
0x1F you are at 31.

And it kind of repeats itself and then goes,
0x20 for 32.

I can do that if I count them manually, I found a few formulas but i am not with it yet.

I see that 0x1 to 0xFF goes to 255,
and what’s also amazing is that 0xFFFFFF goes to 16.7 million, and that must be how they count screen colors too.

It seems like a VERY smart way to contain so many options in such a short string!

Octal means base 8. Instead of 0-9 you only have 0-7 to work with. Therefore 010 is 8

The preceding zero tells PHP that it’s an octal number.
if it had 0x in front of it, it would be base-16.

Can you guess what the value would be then? Of 0x10? :slight_smile:

To explain it better

Take a decimal number (normal numbers, which is base 10)
e.g. 1234

is split into

thousands (10^3), hundreds (10^2), tens (10^1) and ones (10^0)

to work out the number “1234” you’d do:

1 x 1000
2 x 100
3 x 10
4 x 1

and sum the results.

In octal, base 8, the columns are different, instead of 10 ^ n it’s 8 ^ n so…

Lets take the same number, 1234

we have

1 x (8 ^ 3) = 1 x 512 = 512
2 x (8 ^ 2) = 2 x 64 = 128
3 x (8 ^ 1) = 3 x 8 = 24
4 x (8 ^ 0) = 4 x 1 = 4

Add up all the columns and you get 668.

Hope that makes sense :slight_smile:

okay thanks lol

There are 10 types of people in the world - those who understand binary and those who don’t.

And a number of others do. For example JavaScript works exactly the same way as PHP with a leading 0 indicating octal and a leading 0x indicating hexadecimal in its number base conversion functions parseInt and parseFloat if a second parameter to specify which base you are using is not supplied.

Octal was used a lot as the intermediate numbering system with many early computers as an easier to follow option for the programmers than the binary the computer was using. It was later decided that hexadecimal made things even easier and so octal is not really used much any more whereas hexadecimal is used with all sorts of things that involve computers (eg. ip addresses are 8 hexadecimal digits long - eg 7f000001 as the address for localhost - and CSS colour codes are usually specified using six hexadecimal digits - 2 each for red, green, and blue)

It is not a trick, but how php handles number starting from zero (0) being octal.
So, it appears on several PHP interview questions.

This could be a flaw to PHP. If a programmer or user mistypes the input, it becomes octal, not decimal, and your expectations fly away. You are likely to miss the decimal dot as well (.) as in 0.5 being 05.

Several other applications don’t work like this.

The big gotcha with octal is to remember that when PHP encounters a value it tries to convert it to a number and when it sees the 0 first it assesses it as being octal.

It does not, like us see 010 as, oh, drop the 0, its ten.

Whereas when it encouters “010” in quotes, it guesses you actually quoted this on purpose so it must be a string.

All these gotchas jump up and will bite you badly when you try to start to add numbers to strings.

When you can see this even might happen, then its time to read up on type-setting and type-juggling or you are into a world of nasty bugs.

Watch out when mixing numbers and strings.