Difficulty: Medium
Correct Answer: In PHP, a numeric literal with a leading zero such as 0123 is interpreted as an octal (base 8) number, so 0123 is not decimal one hundred twenty three but an octal value that corresponds to a different decimal number.
Explanation:
Introduction / Context:
This question highlights a classic pitfall related to numeric literals with leading zeros in many C style languages, including older behaviour in PHP. When you write a number like 0123 in code, PHP may interpret it differently from what you expect if you assume it is decimal. Understanding how different numeric bases are represented helps avoid subtle bugs, especially when dealing with identifiers, file permissions, or values copied from other sources.
Given Data / Assumptions:
Concept / Approach:
In older PHP versions, and in many related languages, a leading zero in an integer literal indicates that the number is written in octal (base 8). For example, 012 is octal for decimal 10. Octal uses digits 0 through 7, and each position represents powers of 8 rather than powers of 10. Therefore 0123 in octal has a different decimal value than 123 in decimal. If you intend decimal one hundred twenty three, you should write 123 without a leading zero. This behaviour explains why the output seems unexpected when you treat 0123 as if it were decimal.
Step-by-Step Solution:
Step 1: Recognise that 0123 has a leading zero, which is a pattern used historically for octal literals.Step 2: Recall that in octal, each digit position represents powers of 8, so 0123 base 8 equals 1*64 + 2*8 + 3*1.Step 3: Compute 1*64 + 2*8 + 3*1 = 64 + 16 + 3 = 83, which is the decimal value produced.Step 4: Compare this with decimal 123 and note that they are not the same, which explains the confusion.Step 5: Conclude that the problem is the unintended use of an octal literal due to the leading zero, which is what option A states.
Verification / Alternative check:
You can verify this by writing code that assigns $x = 0123; and then echoing $x. The output will be 83 in many PHP versions that still support octal notation in this way. If you instead assign $x = 123; the output will be 123. This experiment confirms that the interpretation of 0123 as octal is the root cause of the mismatch.
Why Other Options Are Wrong:
Option B invents a rule about adding one to numbers that start with 0, which does not exist. Option C incorrectly links the last digit 3 to hexadecimal parsing, which is not how PHP chooses numeric bases. Option D claims the literal is treated as floating point and rounded, which does not match how integer literals like 0123 are handled.
Common Pitfalls:
Developers often copy values such as account numbers or codes that happen to start with zero into code without realising that the leading zero has special meaning. Another common issue is with file permissions, where octal notation is intentional but still confusing. A good rule is to avoid leading zeros for decimal literals and to be explicit when you intend octal or hexadecimal by using clear notation and comments.
Final Answer:
In PHP, a numeric literal with a leading zero such as 0123 is interpreted as an octal (base 8) number, so 0123 is not decimal one hundred twenty three but an octal value that corresponds to a different decimal number.
Discussion & Comments