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";
$newText = substr($text, 2, 10);
echo $newText;
Asubstr() length exceeds string length but no error occurs
Bsubstr() function name is misspelled
CMissing semicolon after echo statement
DVariable $text is not defined
Step-by-Step Solution
Solution:
  1. Step 1: Check substr() parameters

    substr() extracts substring starting at index 2, length 10, but string length is only 5.
  2. Step 2: Understand PHP behavior

    PHP returns substring from index 2 to end without error even if length exceeds string length.
  3. Final Answer:

    substr() length exceeds string length but no error occurs -> Option A
  4. Quick Check:

    substr() handles length > string length gracefully [OK]
Quick Trick: substr() safely handles length longer than string [OK]
Common Mistakes:
  • Expecting an error when length is too long
  • Misspelling function names
  • Forgetting semicolons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes