Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

related to php arithmetic

Tags:

php

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

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

like image 826
Poonam Bhatt Avatar asked Apr 09 '26 00:04

Poonam Bhatt


2 Answers

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

like image 107
darkphoenix Avatar answered Apr 10 '26 13:04

darkphoenix


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.

like image 29
Haim Evgi Avatar answered Apr 10 '26 14:04

Haim Evgi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!