PHP arithmetic operation where number starts with zero -
i have following code:
$a = 012; echo $a/4; echo '<br>'; echo $a+4; echo '<br>'; echo $a; this generates following output:
2.5 14 10 however, expect:
3 16 12 why happen when $a starts 0?
as specified @rizier123 must follow integer type doc.
as specified in official documentation of integers type http://php.net/manual/en/language.types.integer.php
integers can specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded sign (- or +)
$n = 123; // decimal number $n = -123; // negative number $n = 0234; // octal number ... so in case first number $a = 012 means it's in octal notation.
when division: $a/4 php proceed type juggling converting decimal base , dividing 4
$a = 012; // 10 in decimal base echo $a/4; // 10/4 -> 2.5 note: prior php 7, if invalid digit given in octal integer (i.e. 8 or 9), rest of number ignored
Comments
Post a Comment