0
0
PHPprogramming~10 mins

Why arrays are essential in PHP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an array with three colors.

PHP
$colors = array([1]);
Drag options to blanks, or click blank then click option'
A["red", "green", "blue"]
B"red" "green" "blue"
C"red", "green", "blue"
Dred, green, blue
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting commas between items
Not using quotes around string values
Using square brackets inside array()
2fill in blank
medium

Complete the code to access the first element of the array.

PHP
echo $colors[1];
Drag options to blanks, or click blank then click option'
A[0]
B(0)
C{0}
D[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the first element
Using parentheses or curly braces instead of square brackets
3fill in blank
hard

Fix the error in the code to add a new element to the array.

PHP
$colors[1] = "yellow";
Drag options to blanks, or click blank then click option'
A[]
B[3]
Cadd
D(3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' which is not valid syntax
Using specific indexes which may cause errors if index exists
Using parentheses instead of square brackets
4fill in blank
hard

Fill both blanks to create an associative array with keys and values.

PHP
$person = array([1] => "John", [2] => 25);
Drag options to blanks, or click blank then click option'
A"name"
B"age"
C"John"
D25
Attempts:
3 left
💡 Hint
Common Mistakes
Using values as keys
Not using quotes around string keys
5fill in blank
hard

Fill all three blanks to loop through the array and print keys and values.

PHP
foreach ($person as [1] => [2]) {
    echo [3] . ": " . [2] . "\n";
}
Drag options to blanks, or click blank then click option'
A$key
B$value
C$item
D$person
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable for key and value
Using incorrect variable names
Not using the key in the echo statement