In PHP, I haved tried this code print 08+"51";
but I don't know why it gives 51, while print 07+"51";
give 58 ?
Asked
Active
Viewed 66 times
1

John Conde
- 217,595
- 99
- 455
- 496

Van Minh Nhon TRUONG
- 99
- 1
- 6
-
1See http://php.net/manual/en/language.types.integer.php: `$a = 0123; // octal number (equivalent to 83 decimal)` – Stephan Vierkant Jul 09 '18 at 14:31
-
The upvote button states "this question shows research effort". No research effort is obvious yet it stands with 4 upvotes. – castis Jul 09 '18 at 14:34
-
08 doesn't exist in octal – CSSBurner Jul 09 '18 at 14:54
-
How did you not get Fatal Error? – CSSBurner Jul 09 '18 at 14:55
-
@DavidPartyka: Because 08 is converted to 0, and "51" is converted to 51, so it gives 0+51 = 51 – Van Minh Nhon TRUONG Jul 09 '18 at 14:59
1 Answers
8
Because when an integer starts with a 0 you are using octal. 08 is not a valid octal number so it translates to zero. 0 + 51 (because "51" is converted to a integer thanks to type juggling) equals 51.

John Conde
- 217,595
- 99
- 455
- 496
-
1It should be "converted to an integer" not "converted to a string" -- it is already a string – johannes Jul 09 '18 at 14:32
-
-
@johannes You are correct. I changed how I wanted to explain it halfway through and in the end got a bit of both. Fixed. – John Conde Jul 09 '18 at 14:52