Bird
0
0

Identify the error in this PHP code:

medium📝 Debug Q6 of 15
PHP - String Functions
Identify the error in this PHP code:
$text = "Hello World";
echo substr($text, 5, -3);
AStart index 5 is out of range
BNegative length is invalid in substr
CMissing third parameter in substr
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Recall substr length behavior

    PHP substr supports negative length: it omits that many characters from the end of the string (after the start position).
  2. Step 2: Analyze the code

    substr($text, 5, -3) on "Hello World" (len 11) starts at index 5 (' ') and omits 3 chars from end, outputting " Wo". No error.
  3. Final Answer:

    No error, code runs fine -> Option D
  4. Quick Check:

    Negative length omits from end, code runs [OK]
Quick Trick: Negative length in substr omits chars from end [OK]
Common Mistakes:
  • Believing negative length is invalid
  • Confusing negative length with negative start
  • Expecting runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes