0
0
PHPprogramming~5 mins

Array count and length in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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]?
A1
B3
C4
DError
Which PHP function is an alias of count()?
Alength()
Bsizeof()
Carray_length()
Dsize()
What will count(null) return in PHP?
A0
B1
Cnull
DError
If $arr = ['a' => 1, 'b' => 2], what does count($arr) return?
A2
B1
C0
DError
Which of these is NOT a valid way to get the number of elements in an array in PHP?
Acount($array)
Bsizeof($array)
CBoth A and B
Dlength($array)
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.