Recall & Review
beginner
What function in PHP is used to count the number of elements in an array?
The
count() function returns the number of elements in an array.Click to reveal answer
beginner
What will
count([]) return in PHP?It returns
0 because the array is empty and has no elements.Click to reveal answer
beginner
How does PHP treat the length of an array compared to the count of an array?
In PHP, the length of an array is the same as the count of its elements. Use
count() to get this number.Click to reveal answer
intermediate
What is the difference between
count() and sizeof() in PHP?Both
count() and sizeof() return the number of elements in an array. They are aliases and work the same way.Click to reveal answer
intermediate
What happens if you use
count() on a variable that is not an array?If the variable is not an array or an object implementing Countable,
count() returns 1 if the variable is not null, otherwise 0.Click to reveal answer
What does
count($arr) return if $arr = [1, 2, 3, 4]?✗ Incorrect
count() returns the number of elements, which is 4 here.Which PHP function is an alias of
count()?✗ Incorrect
sizeof() is an alias of count() in PHP.What will
count(null) return in PHP?✗ Incorrect
count(null) returns 0 because null is treated as empty.If
$arr = ['a' => 1, 'b' => 2], what does count($arr) return?✗ Incorrect
Associative arrays count their elements the same as indexed arrays.
Which of these is NOT a valid way to get the number of elements in an array in PHP?
✗ Incorrect
length() is not a PHP function for arrays.Explain how to find the number of elements in a PHP array and what happens if the variable is not an array.
Think about the function count() and what it returns for different types.
You got /3 concepts.
Describe the difference, if any, between the length of an array and the count of an array in PHP.
Consider how PHP measures array size.
You got /3 concepts.