Challenge - 5 Problems
Array Count Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of count() with nested arrays
What is the output of the following PHP code?
PHP
<?php $array = [1, 2, [3, 4], 5]; echo count($array); ?>
Attempts:
2 left
💡 Hint
count() counts the top-level elements only.
✗ Incorrect
The array has four elements: 1, 2, [3,4], and 5. The nested array counts as one element.
❓ Predict Output
intermediate2:00remaining
Difference between count() and sizeof()
What will this PHP code output?
PHP
<?php $arr = ['a', 'b', 'c']; echo count($arr) . ' ' . sizeof($arr); ?>
Attempts:
2 left
💡 Hint
sizeof() is an alias of count() in PHP.
✗ Incorrect
Both count() and sizeof() return the number of elements in the array, which is 3.
❓ Predict Output
advanced2:00remaining
Count with COUNT_RECURSIVE flag
What is the output of this PHP code?
PHP
<?php $array = [1, 2, [3, 4], 5]; echo count($array, COUNT_RECURSIVE); ?>
Attempts:
2 left
💡 Hint
COUNT_RECURSIVE counts all elements including nested arrays.
✗ Incorrect
The array has 4 top-level elements, but the nested array has 2 elements, so total is 4 + 2 = 6.
❓ Predict Output
advanced2:00remaining
Length of string vs count() on string
What will this PHP code output?
PHP
<?php $str = "hello"; echo count($str); ?>
Attempts:
2 left
💡 Hint
count() on a string returns 1 because string is not an array.
✗ Incorrect
count() returns 1 when given a non-array, non-countable variable like a string.
🧠 Conceptual
expert2:00remaining
Why does count() return 1 on a string?
In PHP, why does count() return 1 when called on a string variable?
Attempts:
2 left
💡 Hint
Think about how count() handles non-array inputs.
✗ Incorrect
count() returns 1 for any scalar (non-array) value, treating it as a single element.