Bird
0
0

Identify the problem in this PHP code:

medium📝 Debug Q7 of 15
PHP - Arrays
Identify the problem in this PHP code:
$arr = [1, 2, 3];
echo count($arr[3]);
Acount() cannot be used on array elements
BNo problem, code runs fine
CMissing semicolon after count()
DAccessing undefined index before count()
Step-by-Step Solution
Solution:
  1. Step 1: Check array index usage

    The code tries to access $arr[3], but the array has indexes 0,1,2 only.
  2. Step 2: Understand error cause

    Accessing $arr[3] returns NULL, so count(NULL) causes a warning or error.
  3. Final Answer:

    Accessing undefined index before count() -> Option D
  4. Quick Check:

    Access valid indexes before count() [OK]
Quick Trick: Check array indexes exist before counting elements [OK]
Common Mistakes:
  • Counting undefined elements
  • Ignoring array index bounds
  • Assuming count() works on NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes