Challenge - 5 Problems
String Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of string length function
What is the output of this PHP code?
$text = "Hello, world!";
echo strlen($text);
PHP
$text = "Hello, world!"; echo strlen($text);
Attempts:
2 left
💡 Hint
strlen counts all characters including punctuation and spaces.
✗ Incorrect
The string "Hello, world!" has 13 characters including the comma and exclamation mark.
❓ Predict Output
intermediate2:00remaining
Result of string replace
What will this PHP code output?
$str = "I love cats.";
echo str_replace("cats", "dogs", $str);
PHP
$str = "I love cats."; echo str_replace("cats", "dogs", $str);
Attempts:
2 left
💡 Hint
str_replace replaces all occurrences of the search string.
✗ Incorrect
The word "cats" is replaced by "dogs" in the string.
❓ Predict Output
advanced2:00remaining
Output of string position function
What does this PHP code output?
$phrase = "Find the needle in the haystack.";
echo strpos($phrase, "needle");
PHP
$phrase = "Find the needle in the haystack."; echo strpos($phrase, "needle");
Attempts:
2 left
💡 Hint
strpos returns the position of the first character of the found substring, starting at 0.
✗ Incorrect
The word "needle" starts at the 9th position (counting from 0).
❓ Predict Output
advanced2:00remaining
Behavior of substr with negative start
What will this PHP code output?
$text = "Programming";
echo substr($text, -3);
PHP
$text = "Programming"; echo substr($text, -3);
Attempts:
2 left
💡 Hint
A negative start in substr counts from the end of the string.
✗ Incorrect
substr with -3 returns the last 3 characters of the string.
❓ Predict Output
expert2:00remaining
Output of chained string functions
What is the output of this PHP code?
$input = " Hello World ";
echo strtoupper(trim($input));
PHP
$input = " Hello World "; echo strtoupper(trim($input));
Attempts:
2 left
💡 Hint
trim removes spaces, strtoupper converts to uppercase.
✗ Incorrect
trim removes spaces around the string, then strtoupper converts it to uppercase.