0
0
PHPprogramming~20 mins

String length and counting in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A3
B4
C6
D5
Attempts:
2 left
💡 Hint
strlen counts bytes, not characters, in PHP.
Predict Output
intermediate
2: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");
A4
B2
C1
D3
Attempts:
2 left
💡 Hint
Count how many times 'l' appears in "hello world".
Predict Output
advanced
2: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);
A6,5
B5,5
C6,6
D5,6
Attempts:
2 left
💡 Hint
strlen counts bytes, mb_strlen counts characters.
Predict Output
advanced
2: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");
A3
B5
C2
D4
Attempts:
2 left
💡 Hint
substr_count does not count overlapping substrings.
🧠 Conceptual
expert
2:00remaining
Why use mb_strlen over strlen?
Which statement best explains why mb_strlen is preferred over strlen for UTF-8 strings in PHP?
Amb_strlen is faster than strlen for all strings.
Bmb_strlen counts the number of characters correctly in multibyte strings, while strlen counts bytes.
Cstrlen can only be used with ASCII strings, mb_strlen only with UTF-16 strings.
Dstrlen automatically converts strings to lowercase before counting.
Attempts:
2 left
💡 Hint
Think about how UTF-8 characters can use multiple bytes.