0

Without wanting to sollicate the obvious Q&A, I'm wondering if anybody knows why the following happens.

I have a string variable $string with the value '1011100001111'

Testing for a '1' or '0' at a certain position:

$string[00] => '1'  // correct
$string[01] => '0'  // correct
$string[07] => '0'  // correct
$string[08] => '1'  // should be '0'
$string[ 8] => '0'  // correct

It took me a while to find the error when using prefix zeroes, and obviously I got rid of them, but why is index 08 different from 8? Note that up to 07 it does work!

4

2 Answers 2

5

From the documentation regarding integers:

To use octal notation, precede the number with a 0 (zero)

So 08 is not the integer 8 and therefore not returning the index you expect. It's an invalid octal and resolves to 0.

Sign up to request clarification or add additional context in comments.

2 Comments

But There is good news. In PHP7 it will generate a Parse Error
Thanks for clearing that up. Makes me wonder what the result is if I use 008. I will try and find out.
0

If you precede the numbers with 0, they will be considered octal numbers. So from 00 to 07, they are equivalent to 0-7 decimal. But 08 does not exist in octal so it resolves to 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.