user3317582
user3317582

Reputation: 19

Why this code's result is 37?

Why this code's result is 37?

$a = 11 + 011 + 0x11;
var_dump($a);//result = 37

Upvotes: 0

Views: 1502

Answers (2)

Nick Parsons
Nick Parsons

Reputation: 50874

The result is 37 as the sum of all the numbers evaluates to 37.

011 - This is considered an octal value (as it begins in 0), thus is equates to 9.

0x11 - This is considered a hex value, thus it equates to 17

Hence:

11 + 011 + 0x11,

Can be written as:

11 + 9 + 17 = 37

Perhaps take a look at the PHP Manual

Upvotes: 0

Akshay Hegde
Akshay Hegde

Reputation: 16997

$a = 11    +   011    +   0x11;
     ^          ^          ^
  base 10    base 8       base 16
    11     +   9      +    17          = 37

Base 16

0x11 =   1 * 16^1 (16)
       + 1 * 16^0 ( 1)
     -----------------
      0x11        (17)

Base 8

11 = 1×8^1 + 1×8^0 = 8+1 = 9

Table

base 8  Decimal 
0        0
1        1
2        2
3        3
4        4
5        5
6        6
7        7
10       8
11       9            -> Here
12       10
13       11

Upvotes: 2

Related Questions