Bird
0
0

Identify the error in this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Array Functions
Identify the error in this PHP code snippet:
$data = ['color' => 'red', 'size' => 'large'];
extract($data);
echo $colour;
AVariable name mismatch: $colour vs 'color' key
Bextract() cannot be used on associative arrays
CMissing semicolon after extract()
Decho cannot print variables created by extract()
Step-by-Step Solution
Solution:
  1. Step 1: Check variable names created by extract()

    The extract function creates variables named exactly as the array keys, so it creates $color and $size.
  2. Step 2: Compare echoed variable name

    The code tries to echo $colour which is different from $color, causing an undefined variable notice.
  3. Final Answer:

    Variable name mismatch: $colour vs 'color' key -> Option A
  4. Quick Check:

    extract keys = variable names; spelling must match [OK]
Quick Trick: Check spelling of variable names after extract [OK]
Common Mistakes:
  • Assuming extract changes variable names
  • Thinking extract only works on indexed arrays
  • Ignoring PHP notices for undefined variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes