Bird
0
0

Find the error in this PHP code snippet:

medium📝 Debug Q7 of 15
PHP - Loops
Find the error in this PHP code snippet:
$data = ['x' => 5, 'y' => 10];
foreach ($data as $key, $value) {
  echo $key . $value;
}
AIncorrect array declaration
BMissing semicolon after echo
CUsing comma instead of => in foreach
DUsing wrong variable names
Step-by-Step Solution
Solution:
  1. Step 1: Review foreach syntax for keys and values

    It should be foreach ($array as $key => $value), not comma separated.
  2. Step 2: Identify the incorrect use of comma

    The code uses a comma between $key and $value, which causes a syntax error.
  3. Final Answer:

    Using comma instead of => in foreach -> Option C
  4. Quick Check:

    Use '=>' not ',' for key-value in foreach [OK]
Quick Trick: Use '=>' to separate key and value in foreach [OK]
Common Mistakes:
  • Using comma instead of =>
  • Forgetting semicolons
  • Wrong array syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes