Bird
Raised Fist0
PHPprogramming~10 mins

Array count and length in PHP - Interactive Code Practice

Choose your learning style10 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'.

Practice

(1/5)
1. What does the PHP function count() do when used on an array?
easy
A. It returns the number of elements in the array.
B. It returns the sum of all elements in the array.
C. It returns the last element of the array.
D. It sorts the array in ascending order.

Solution

  1. Step 1: Understand the purpose of count()

    The count() function in PHP is designed to return how many items are inside an array.
  2. Step 2: Compare with other options

    The other options describe summing elements, returning the last element, or sorting the array, which are unrelated to counting elements.
  3. Final Answer:

    It returns the number of elements in the array. -> Option A
  4. Quick Check:

    count() = number of elements [OK]
Hint: Remember: count() tells how many items, not their values. [OK]
Common Mistakes:
  • Thinking count() sums values instead of counting items
  • Confusing count() with functions that return elements
  • Assuming count() sorts or changes the array
2. Which of the following is the correct syntax to get the number of elements in an array $arr in PHP?
easy
A. length($arr);
B. sizeofArray($arr);
C. size($arr);
D. count($arr);

Solution

  1. Step 1: Recall PHP function for array length

    PHP uses count() to get the number of elements in an array.
  2. Step 2: Check other options for validity

    Functions like length(), size(), or sizeofArray() do not exist or are incorrect in PHP.
  3. Final Answer:

    count($arr); -> Option D
  4. Quick Check:

    Use count() for array size [OK]
Hint: Use count() to get array size, not length() or size(). [OK]
Common Mistakes:
  • Using length() which is not a PHP function
  • Confusing count() with size() or sizeofArray()
  • Adding semicolon inside function parentheses
3. What will be the output of the following PHP code?
$fruits = ['apple', 'banana', 'cherry'];
echo count($fruits);
medium
A. 0
B. 3
C. applebanana cherry
D. Error

Solution

  1. Step 1: Identify the array elements

    The array $fruits contains three elements: 'apple', 'banana', and 'cherry'.
  2. Step 2: Apply count() function

    The count() function returns the number of elements, which is 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    count(['apple','banana','cherry']) = 3 [OK]
Hint: Count returns number of items, not their values. [OK]
Common Mistakes:
  • Expecting count() to print array contents
  • Confusing count() with string concatenation
  • Thinking count() returns 0 for non-empty arrays
4. Consider this PHP code snippet:
$numbers = [1, 2, 3, 4];
$count = count($numbers
echo $count;
What is the problem with this code?
medium
A. Missing semicolon after count() function call.
B. count() cannot be used on numeric arrays.
C. Missing closing parenthesis in count() function call.
D. Variable $count is not defined before use.

Solution

  1. Step 1: Check syntax of count() call

    The code count($numbers is missing a closing parenthesis ")".
  2. Step 2: Verify other syntax elements

    There is no semicolon after the count() call, but the bigger issue is the missing parenthesis which causes a syntax error first.
  3. Final Answer:

    Missing closing parenthesis in count() function call. -> Option C
  4. Quick Check:

    count() needs matching parentheses [OK]
Hint: Always check parentheses balance in function calls. [OK]
Common Mistakes:
  • Ignoring missing parenthesis and blaming semicolon
  • Thinking count() can't be used on numeric arrays
  • Assuming variable must be declared before assignment
5. You have an associative array in PHP:
$person = ['name' => 'Alice', 'age' => 30, 'city' => 'Paris'];
Which code correctly checks if the array has exactly 3 elements before printing "Valid"?
hard
A. if (count($person) == 3) { echo "Valid"; }
B. if (sizeof($person) > 3) { echo "Valid"; }
C. if (count($person) < 3) { echo "Valid"; }
D. if (length($person) == 3) { echo "Valid"; }

Solution

  1. Step 1: Understand the array and count() usage

    The array $person has 3 key-value pairs. Using count() returns the number of elements.
  2. Step 2: Check the condition for exactly 3 elements

    if (count($person) == 3) { echo "Valid"; } correctly uses count($person) == 3 to check if the array has exactly 3 elements.
  3. Final Answer:

    if (count($person) == 3) { echo "Valid"; } -> Option A
  4. Quick Check:

    count() == 3 means array has 3 elements [OK]
Hint: Use count() == number to check exact array size. [OK]
Common Mistakes:
  • Using sizeof() with wrong comparison operator
  • Using length() which is not a PHP function
  • Checking for less or greater instead of exact count
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard