0
0
PHPprogramming~10 mins

Why array functions matter 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 get the number of elements in the array.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
$count = [1]($fruits);
echo $count;
?>
Drag options to blanks, or click blank then click option'
Acount
Bstrlen
Csizeof
Darray_length
Attempts:
3 left
💡 Hint
Common Mistakes
Using strlen() which counts string length, not array elements.
Using array_length which is not a PHP function.
2fill in blank
medium

Complete the code to add a new element 'orange' to the end of the array.

PHP
<?php
$fruits = ['apple', 'banana'];
array_[1]($fruits, 'orange');
print_r($fruits);
?>
Drag options to blanks, or click blank then click option'
Ashift
Bpop
Cpush
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_pop() which removes the last element.
Using array_shift() which removes the first element.
3fill in blank
hard

Fix the error in the code to check if 'banana' is in the array.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
if ([1]('banana', $fruits)) {
    echo 'Found banana';
} else {
    echo 'Banana not found';
}
?>
Drag options to blanks, or click blank then click option'
Aarray_key_exists
Bin_array
Carray_search
Disset
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_key_exists which checks keys, not values.
Using isset which checks if a variable is set.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers 1 to 5.

PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$squares = array_map(function([1]) {
    return [2] * [1];
}, $numbers);
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A$n
C$num
D$number
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside and outside the function argument.
Forgetting to multiply the variable by itself.
5fill in blank
hard

Fill all three blanks to filter numbers greater than 3 and create a new array with those numbers doubled.

PHP
<?php
$nums = [1, 2, 3, 4, 5];
$filtered = array_filter($nums, function([1]) {
    return [2] > 3;
});
$doubled = array_map(function([3]) {
    return [3] * 2;
}, $filtered);
print_r($doubled);
?>
Drag options to blanks, or click blank then click option'
A$num
C$n
D$val
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently inside the functions.
Not returning the correct condition in the filter function.