Php related arithmetic

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

I can not understand how its ans 16? Can anyone help?

+2
source share
3 answers

because 013 is not decimal (base 10). it is octal (base 8). decimal value: (0 * 8 ^ 2) + (1 * 8 ^ 1) + (3 * 8 ^ 0) = 0 + 8 + 3 = 11

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

moral of the story: don't add a numeric literal with 0 unless you know what that means

+6
source

a number with a leading zero is an octal number

as

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

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

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

+4
source

$ y = 013; echo $ y + 5;

013 is an octal number. The whole number of php integers is octal.

show this link. first.

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

0
source

Source: https://habr.com/ru/post/1338982/


All Articles