Which of the following creates an indexed array with values 10, 20, 30 in PHP?
A$numbers = <10, 20, 30>;
B$numbers = {10, 20, 30};
C$numbers = [10, 20, 30];
D$numbers = (10, 20, 30);
✗ 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'];?
A1
B0
C2
D3
✗ 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'];?
A$fruits['grape'] = true;
B$fruits[] = 'grape';
Carray_push($fruits, 'grape');
DBoth C and D
✗ 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'];?
A2
B3
C0
DError
✗ 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?
A$arr = {1, 2, 3};
B$arr = [1, 2, 3];
C$arr = array(1, 2, 3);
D$arr = [];
✗ 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.
Practice
(1/5)
1. Which of the following correctly creates an indexed array with three elements in PHP?
easy
A. $arr = ["apple", "banana", "cherry"];
B. $arr = {"apple", "banana", "cherry"};
C. $arr = ("apple", "banana", "cherry");
D. $arr = array:"apple", "banana", "cherry";
Solution
Step 1: Recognize PHP array syntax
PHP indexed arrays can be created using square brackets [] or the array() function.
Step 2: Check each option's syntax
$arr = ["apple", "banana", "cherry"]; uses [] with comma-separated values, which is correct. Options A, B, and C use invalid syntax for arrays.