-5
echo (int)01; //1

echo (int)02; //2

echo (int)03; //3

echo (int)04; //4

echo (int)05; //5

echo (int)06; //6

echo (int)07; //7

echo (int)08; //0

echo (int)09; //0

echo (int)010; //8

echo (int)011; //9

echo (int)012; //10

echo (int)013; //11

(int) was doing right from 01 to 07. But after that it goes wrong. What's the reason of it??

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    Read about [numbers](http://php.net/manual/en/language.types.integer.php#language.types.integer.syntax). – axiac Jan 23 '18 at 07:14

2 Answers2

1

If a number starts with 0 it is consider an octal number

and since these numbers range from 0 to 7 only. You get a 0 since 8%8 = 0

Reference: http://php.net/manual/en/language.types.integer.php

A side note on number systems

Have you wondered why the next number to 9 is 10? and why in the binary system the sequence is 0, 1, 10, 11... ? And why the octal system allows only values from 0 to 7?

It is because number systems usually increment values based on modulo logic on the base.

For example take the binary number system. Since it is base 2, it can only contain values 0, 1 since 0%2 = 0, 1%2 = 1, but 2%2 is again 0

So whenever a greater number comes, say 3. Its value in binary is (increment by one in the preceding place) (put the modulo in the existing place)

So the value of 3 in the binary system is (0+1) (3%2) = 11

Though this is not the exact logic, just putting it here for a beginner reference

S Raghav
  • 1,386
  • 1
  • 16
  • 26
1

Perhaps 08 is expected to be octal number, like 0x is hexadecimal.

ziza
  • 96
  • 1
  • 6