0
0
PHPprogramming~20 mins

Trim functions in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trim Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
?>
A" Hello World!"
B" Hello World! \n"
C"Hello World! \n"
D"Hello World!"
Attempts:
2 left
💡 Hint
trim() removes whitespace from the start and end of a string.
Predict Output
intermediate
2:00remaining
Using trim() with custom character mask
What will this PHP code output?
PHP
<?php
$str = "xxxyHello World!yyx";
echo trim($str, "xy");
?>
A"Hello World!"
B"xyHello World!yyx"
C"Hello World!yyx"
D"xxxyHello World!"
Attempts:
2 left
💡 Hint
trim() removes all characters in the mask from both ends.
Predict Output
advanced
2: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) . ']';
?>
A[ Hello World! ][ Hello World! ][ Hello World! ]
B[ Hello World!][Hello World! ][Hello World!]
C[Hello World! ][ Hello World!][Hello World!]
D[Hello World!][Hello World!][Hello World!]
Attempts:
2 left
💡 Hint
ltrim() removes from left, rtrim() from right, trim() from both ends.
Predict Output
advanced
2:00remaining
Behavior of trim() with empty string mask
What will this PHP code output?
PHP
<?php
$str = "  Hello World!  ";
echo trim($str, "");
?>
A" Hello World! "
B"Hello World!"
CSyntaxError
D"Hello World! "
Attempts:
2 left
💡 Hint
An empty mask means no characters are removed.
🧠 Conceptual
expert
3: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?
Atrim() removes characters only if they appear in the exact order 'abc' at the string ends.
Btrim() removes any combination of 'a', 'b', or 'c' characters from the start and end, in any order, until a character not in the mask is found.
Ctrim() removes only the first character if it matches 'a', then stops.
Dtrim() removes characters only from the middle of the string if they match 'a', 'b', or 'c'.
Attempts:
2 left
💡 Hint
Think about how trim() scans from both ends.