Bird
0
0

Identify the error in the following PHP code that tries to create an indexed array:

medium📝 Debug Q14 of 15
PHP - Arrays
Identify the error in the following PHP code that tries to create an indexed array:
$colors = array["red", "green", "blue"];
AArray elements must be integers
BMissing semicolon at the end
CUsing double quotes instead of single quotes
DUsing square brackets [] instead of parentheses () with array
Step-by-Step Solution
Solution:
  1. Step 1: Check array() function syntax

    The array() function requires parentheses (), not square brackets [].
  2. Step 2: Identify the syntax error

    The code uses array[ ... ] which is invalid syntax and causes an error.
  3. Final Answer:

    Using square brackets [] instead of parentheses () with array -> Option D
  4. Quick Check:

    array() needs () not [] [OK]
Quick Trick: Use parentheses () with array(), not square brackets [] [OK]
Common Mistakes:
  • Confusing array() with [] syntax
  • Forgetting semicolon (though present here)
  • Thinking quotes type matters for array creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes