Complete the code to create an array with three colors.
$colors = array([1]);In PHP, arrays are created using the array() function with comma-separated values inside quotes for strings.
Complete the code to access the first element of the array.
echo $colors[1];PHP arrays use zero-based indexing, so the first element is accessed with [0].
Fix the error in the code to add a new element to the array.
$colors[1] = "yellow";
To add a new element to the end of an array in PHP, use empty square brackets [].
Fill both blanks to create an associative array with keys and values.
$person = array([1] => "John", [2] => 25);
Associative arrays use keys (like "name" and "age") to map to values.
Fill all three blanks to loop through the array and print keys and values.
foreach ($person as [1] => [2]) { echo [3] . ": " . [2] . "\n"; }
In a foreach loop, $key holds the key and $value holds the value. We print the key and value inside the loop.