Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Arrays
What will be the output of this PHP code?
$items = ['first' => 'a', 'second' => 'b', 'third' => 'c', 'fourth' => 'd'];
echo count($items);
A4
B3
C0
DError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the array

    The associative array $items has 4 key-value pairs.
  2. Step 2: Use count()

    count($items) returns the total number of elements, which is 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    count() counts all elements regardless of keys [OK]
Quick Trick: count() counts all elements including associative keys [OK]
Common Mistakes:
  • Assuming count() counts only numeric keys
  • Thinking count() returns last key
  • Confusing count() with array_key_exists()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes