3

Why does

var_dump(16) // displays int(16) 

but

var_dump(016) // displays int(14) 

Anyone can help me solve this problem?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Nhan Nguyen
  • 197
  • 1
  • 9
  • Because `016` is interpreted as octal number – Rizier123 Aug 09 '16 at 13:11
  • 3
    Because "numbers" beginning with a zero in PHP are treated as octal, and octal 16 is decimal 14.... this is nothing to do with var_dump, and everything to do with how you declare the number - [PHP Documentation](http://php.net/manual/en/language.types.integer.php) – Mark Baker Aug 09 '16 at 13:11

1 Answers1

3

The second value is called octal. It's not the same as base 10. Instead it's base 8. When you add the 0 in front it tells PHP to treat it as an octal.

http://php.net/manual/en/language.types.integer.php

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

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

10 in base 8 is 8

6 in base 8 is 6

8 + 6 = 14

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • ... and, if you *intended* to "use Octal throughout," you'd have to be sure that this result, "fourteen," was displayed in **octal** notation (complete with a leading-zero to indicate that it *is* octal). The absence of a leading zero would indicate that `14` is (base-10) "fourteen," but, "this is a mistake that is very easy to make, *and* very easy for human mortals to overlook when they're trying to debug the thing. *(Yeah, "been there, did that ..." I banged my forehead against that very issue for quite some time before I said: "d'oh!")* – Mike Robinson Aug 09 '16 at 14:20
  • Hewlett-Packard minicomputers (HP1000, HP2000, HP3000, *etc.)* used Octal notation routinely, and *they* used the convention of requiring a preceding percent-sign, "`%`." They would also always write-out the correct number of digits, thus clearly indicating not only what the value was, but how many bits it occupied. Their language-compilers would honor that convention, too. In similar fashion, Hexadecimal *(base-16)* numbers would use a preceding `$` character. I happen to *like* that convention, because it's very human- clear, but not-all languages do things the same way. – Mike Robinson Aug 09 '16 at 14:23