0
0
PHPprogramming~10 mins

Array count and length in PHP - Interactive Code Practice

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

Complete the code to count 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
Blength
Csize
Dtotal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' which is not a PHP function for arrays.
Using 'size' or 'total' which do not exist as PHP functions.
2fill in blank
medium

Complete the code to get the length of the string stored in the array element.

PHP
<?php
$words = ['hello', 'world'];
$length = [1]($words[0]);
echo $length;
?>
Drag options to blanks, or click blank then click option'
Astrlen
Blength
Csize
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' which counts array elements, not string length.
Using 'size' or 'length' which are not PHP functions.
3fill in blank
hard

Fix the error in the code to correctly count the elements in the array.

PHP
<?php
$numbers = [1, 2, 3, 4];
$total = [1]($numbers);
echo $total;
?>
Drag options to blanks, or click blank then click option'
Asize
Bcount
Cstrlen
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which is not a PHP function.
Using strlen() on arrays causes errors.
4fill in blank
hard

Fill both blanks to create an associative array with word lengths for words longer than 3 characters.

PHP
<?php
$words = ['cat', 'house', 'dog', 'elephant'];
$lengths = [];
foreach($words as [1]) {
  if(strlen([1]) > 3) {
    $lengths[[1]] = [2];
  }
}
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
A$word
Bstrlen('word')
C'length'
Dstrlen($word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of variables for keys.
Using strlen('word') which returns length of the literal 'word', not the variable.
5fill in blank
hard

Fill all three blanks to create a filtered array of words longer than 4 characters with their lengths.

PHP
<?php
$words = ['sun', 'moon', 'stars', 'sky'];
$result = array_filter(array_map(function([1]) {
    return ['[2]' => [1], '[3]' => strlen([1])];
}, $words), function($item) {
    return $item['length'] > 4;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A$word
Bword
Clength
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without $ in function parameters.
Using incorrect keys like 'name' instead of 'word' or 'length'.