Challenge - 5 Problems
Trim Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of trim() with default characters
What is the output of the following PHP code?
PHP
<?php $str = " Hello World! \n"; echo trim($str); ?>
Attempts:
2 left
💡 Hint
trim() removes whitespace from the start and end of a string.
✗ Incorrect
The trim() function removes spaces, tabs, newlines from both ends of the string. So the output is "Hello World!" without spaces or newline around it.
❓ Predict Output
intermediate2:00remaining
Using trim() with custom character mask
What will this PHP code output?
PHP
<?php $str = "xxxyHello World!yyx"; echo trim($str, "xy"); ?>
Attempts:
2 left
💡 Hint
trim() removes all characters in the mask from both ends.
✗ Incorrect
The mask "xy" means trim removes all 'x' and 'y' characters from start and end. So "xxxy" and "yyx" are removed, leaving "Hello World!".
❓ Predict Output
advanced2:00remaining
Difference between trim(), ltrim(), and rtrim()
What is the output of this PHP code?
PHP
<?php $str = " Hello World! "; echo '[' . ltrim($str) . ']'; echo '[' . rtrim($str) . ']'; echo '[' . trim($str) . ']'; ?>
Attempts:
2 left
💡 Hint
ltrim() removes from left, rtrim() from right, trim() from both ends.
✗ Incorrect
ltrim() removes spaces on the left only, so right spaces remain. rtrim() removes spaces on the right only, so left spaces remain. trim() removes spaces on both ends.
❓ Predict Output
advanced2:00remaining
Behavior of trim() with empty string mask
What will this PHP code output?
PHP
<?php $str = " Hello World! "; echo trim($str, ""); ?>
Attempts:
2 left
💡 Hint
An empty mask means no characters are removed.
✗ Incorrect
When the character mask is an empty string, trim() does not remove any characters, so the original string is returned unchanged.
🧠 Conceptual
expert3:00remaining
Why does trim() remove characters in any order from the mask?
Consider trim($str, "abc") in PHP. Which statement best explains how characters are removed?
Attempts:
2 left
💡 Hint
Think about how trim() scans from both ends.
✗ Incorrect
trim() treats the mask as a set of characters. It removes any characters from the start and end that are in the mask, regardless of order, until it finds a character not in the mask.