Challenge - 5 Problems
String Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of strlen with multibyte characters
What is the output of this PHP code?
$str = "café";
echo strlen($str);
PHP
$str = "café"; echo strlen($str);
Attempts:
2 left
💡 Hint
strlen counts bytes, not characters, in PHP.
✗ Incorrect
The string "café" contains the character 'é' which is two bytes in UTF-8. So strlen returns 6 bytes total.
❓ Predict Output
intermediate2:00remaining
Counting occurrences of a character
What does this PHP code output?
$text = "hello world";
echo substr_count($text, "l");
PHP
$text = "hello world"; echo substr_count($text, "l");
Attempts:
2 left
💡 Hint
Count how many times 'l' appears in "hello world".
✗ Incorrect
The letter 'l' appears 3 times in "hello world".
❓ Predict Output
advanced2:00remaining
Difference between strlen and mb_strlen
What is the output of this PHP code?
$str = "naïve";
echo strlen($str) . "," . mb_strlen($str);
PHP
$str = "naïve"; echo strlen($str) . "," . mb_strlen($str);
Attempts:
2 left
💡 Hint
strlen counts bytes, mb_strlen counts characters.
✗ Incorrect
The character 'ï' is two bytes in UTF-8, so strlen returns 6 bytes, but mb_strlen returns 5 characters.
❓ Predict Output
advanced2:00remaining
Counting substring occurrences with overlapping
What is the output of this PHP code?
$text = "aaaaa";
echo substr_count($text, "aa");
PHP
$text = "aaaaa"; echo substr_count($text, "aa");
Attempts:
2 left
💡 Hint
substr_count does not count overlapping substrings.
✗ Incorrect
The substring "aa" appears 3 times without overlapping: positions 0-1, 2-3, and 3-4.
🧠 Conceptual
expert2:00remaining
Why use mb_strlen over strlen?
Which statement best explains why mb_strlen is preferred over strlen for UTF-8 strings in PHP?
Attempts:
2 left
💡 Hint
Think about how UTF-8 characters can use multiple bytes.
✗ Incorrect
strlen counts bytes, so it can give wrong character counts for multibyte UTF-8 strings. mb_strlen counts characters correctly.