Poonam Bhatt
Poonam Bhatt

Reputation: 10302

related to php arithmetic

$y = 013;
echo $y + 5; //this result in 16

I can not figure it out how its ans is 16? Can any one help?

Upvotes: 2

Views: 76

Answers (3)

Karthik
Karthik

Reputation: 1488

$y = 013; echo $y + 5;

013 is octal number all php integer numbers are octal .

show this link. first.

http://www.ascii.cl/conversion.htm

Upvotes: 0

darkphoenix
darkphoenix

Reputation: 2167

because 013 isn't decimal (base 10). it's octal (base 8). the value in decimal is: (0 * 8^2) + (1 * 8^1) + (3 * 8^0) = 0 + 8 + 3 = 11

which gives the correct (though unexpected, at least by you) result of 16 when added to 5.

moral of the story: don't prepend a number literal with 0 unless you know what it means

Upvotes: 6

Haim Evgi
Haim Evgi

Reputation: 125446

number with leading zero is octal number

like

$a = 0123; // octal number (equivalent to 83 decimal

Integers can be specified in decimal (base 10), hexadecimal (base 16), or octal (base 8) notation, optionally preceded by a sign (- or +).

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

Upvotes: 4

Related Questions