Bird
0
0

Given this code:

hard📝 Application Q9 of 15
PHP - Arrays
Given this code:
$arr = ["a" => 1, "b" => 2, "a" => 3];
echo $arr["a"];

What will be the output and why?
A1 because first key wins
B3 because last key overwrites
CError due to duplicate keys
DUndefined because keys conflict
Step-by-Step Solution
Solution:
  1. Step 1: Understand duplicate keys in associative arrays

    PHP overwrites earlier values with later ones for duplicate keys.
  2. Step 2: Identify value for key "a"

    Last value 3 overwrites first 1.
  3. Final Answer:

    3 because last key overwrites -> Option B
  4. Quick Check:

    Duplicate keys keep last value [OK]
Quick Trick: Duplicate keys overwrite earlier values with last one [OK]
Common Mistakes:
  • Expecting error on duplicates
  • Thinking first value stays
  • Confusing keys and values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes