Recall & Review
beginner
What is an indexed array in PHP?
An indexed array in PHP is a list of values where each value is automatically assigned a numeric index starting from 0.
Click to reveal answer
beginner
How do you create an indexed array with three fruits: apple, banana, and cherry?
You can create it like this: <br>
$fruits = array('apple', 'banana', 'cherry'); <br> or using the short syntax: <br> $fruits = ['apple', 'banana', 'cherry'];Click to reveal answer
beginner
What index does the first element of an indexed array have in PHP?
The first element of an indexed array always has the index 0.
Click to reveal answer
beginner
How can you add a new element to the end of an existing indexed array?
You can add a new element by using the empty square brackets: <br>
$fruits[] = 'orange'; <br> This adds 'orange' at the next available numeric index.Click to reveal answer
beginner
What will be the output of
print_r($fruits); if $fruits = ['apple', 'banana']; $fruits[] = 'cherry';?The output will be:<br>
Array
(
[0] => apple
[1] => banana
[2] => cherry
)Click to reveal answer
Which of the following creates an indexed array with values 10, 20, 30 in PHP?
✗ Incorrect
In PHP, indexed arrays are created using square brackets [] or the array() function. Curly braces, parentheses, or angle brackets are not valid for array creation.
What is the index of the element 'banana' in
$fruits = ['apple', 'banana', 'cherry'];?✗ Incorrect
'banana' is the second element, so its index is 1 because indexing starts at 0.
How do you add 'grape' to the end of the array
$fruits = ['apple', 'banana'];?✗ Incorrect
You can add an element by using empty brackets or the array_push() function. Both methods add 'grape' to the end.
What will
count($fruits); return if $fruits = ['apple', 'banana', 'cherry'];?✗ Incorrect
count() returns the number of elements in the array, which is 3 here.
Which syntax is NOT valid for creating an indexed array in PHP?
✗ Incorrect
Curly braces {} are not used for array creation in PHP.
Explain how to create an indexed array in PHP and how the indexes are assigned.
Think about how you list items in order without naming the keys.
You got /3 concepts.
Describe two ways to add a new element to an existing indexed array in PHP.
One way uses brackets, the other is a built-in function.
You got /2 concepts.