Bird
0
0

Which of the following is the correct syntax to create a 2D array in PHP?

easy📝 Syntax Q3 of 15
PHP - Arrays
Which of the following is the correct syntax to create a 2D array in PHP?
A$matrix = {1, 2, 3, 4};
B$matrix = (1, 2), (3, 4);
C$matrix = [[1, 2], [3, 4]];
D$matrix = array(1, 2, 3, 4);
Step-by-Step Solution
Solution:
  1. Step 1: Review PHP array syntax

    PHP uses square brackets [] or array() to create arrays. Nested arrays create multidimensional arrays.
  2. Step 2: Check each option

    $matrix = [[1, 2], [3, 4]]; uses nested square brackets correctly. Options B and C use invalid syntax. $matrix = array(1, 2, 3, 4); creates a single array, not multidimensional.
  3. Final Answer:

    $matrix = [[1, 2], [3, 4]]; -> Option C
  4. Quick Check:

    Nested square brackets create 2D arrays [OK]
Quick Trick: Use nested square brackets for multidimensional arrays [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Using curly braces for arrays
  • Confusing single and multidimensional array syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes