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:
Step 1: Review PHP array syntax
PHP uses square brackets [] or array() to create arrays. Nested arrays create multidimensional arrays.
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.
Final Answer:
$matrix = [[1, 2], [3, 4]]; -> Option C
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
Master "Arrays" in PHP
9 interactive learning modes - each teaches the same concept differently