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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -