0
0
PHPprogramming~20 mins

Array count and length in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Count Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
?>
A3
B5
CError
D4
Attempts:
2 left
💡 Hint
count() counts the top-level elements only.
Predict Output
intermediate
2:00remaining
Difference between count() and sizeof()
What will this PHP code output?
PHP
<?php
$arr = ['a', 'b', 'c'];
echo count($arr) . ' ' . sizeof($arr);
?>
A3 0
B0 3
C3 3
DError
Attempts:
2 left
💡 Hint
sizeof() is an alias of count() in PHP.
Predict Output
advanced
2: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);
?>
A4
B6
C5
DError
Attempts:
2 left
💡 Hint
COUNT_RECURSIVE counts all elements including nested arrays.
Predict Output
advanced
2:00remaining
Length of string vs count() on string
What will this PHP code output?
PHP
<?php
$str = "hello";
echo count($str);
?>
A1
B5
CError
D0
Attempts:
2 left
💡 Hint
count() on a string returns 1 because string is not an array.
🧠 Conceptual
expert
2:00remaining
Why does count() return 1 on a string?
In PHP, why does count() return 1 when called on a string variable?
ABecause count() treats any non-array as a single element
BBecause strings are internally arrays of characters
CBecause count() counts characters in the string
DBecause count() throws an error on strings
Attempts:
2 left
💡 Hint
Think about how count() handles non-array inputs.