Bird
Raised Fist0
PHPprogramming~20 mins

Trim functions in PHP - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the PHP function trim() do to a string?
easy
A. Removes all spaces inside the string
B. Removes whitespace from both the beginning and end of the string
C. Removes only the first character of the string
D. Converts the string to uppercase

Solution

  1. Step 1: Understand the purpose of trim()

    The trim() function removes unwanted whitespace characters from the start and end of a string.
  2. Step 2: Compare options with function behavior

    Only Removes whitespace from both the beginning and end of the string correctly describes removing whitespace from both ends, not inside or changing case.
  3. Final Answer:

    Removes whitespace from both the beginning and end of the string -> Option B
  4. Quick Check:

    trim() removes spaces at both ends [OK]
Hint: trim() cleans spaces only at string ends, not inside [OK]
Common Mistakes:
  • Thinking trim() removes spaces inside the string
  • Confusing trim() with strtoupper()
  • Assuming trim() removes only from one side
2. Which of the following is the correct syntax to remove spaces only from the left side of a string in PHP?
easy
A. ltrim($str)
B. rtrim($str)
C. trim($str)
D. strip($str)

Solution

  1. Step 1: Identify function for left-side trimming

    The function ltrim() removes whitespace from the left (start) side of a string.
  2. Step 2: Check other options

    rtrim() removes from right side, trim() removes both sides, and strip() is not a valid PHP function.
  3. Final Answer:

    ltrim($str) -> Option A
  4. Quick Check:

    ltrim() trims left side only [OK]
Hint: ltrim() trims left side; rtrim() trims right side [OK]
Common Mistakes:
  • Using rtrim() to trim left side
  • Using trim() when only left side is needed
  • Using non-existent function strip()
3. What will be the output of the following PHP code?
$text = "  Hello World!  ";
echo strlen(trim($text));
medium
A. 15
B. 17
C. 13
D. 11

Solution

  1. Step 1: Understand the string and trim effect

    The original string has 2 spaces before and 2 spaces after "Hello World!". The length with spaces is 17.
  2. Step 2: Apply trim() and calculate length

    trim() removes spaces at both ends, so the string becomes "Hello World!" which has 12 characters plus 1 for the exclamation mark, total 12 + 1 = 12? Actually, "Hello World!" is 12 characters? Let's count: H(1) e(2) l(3) l(4) o(5) space(6) W(7) o(8) r(9) l(10) d(11) !(12). So 12 characters total. But the option says 13. The original string is " Hello World! " with 2 spaces before and 2 after, total length 17. After trim, spaces removed, length is 12. So 13 is 13, which is incorrect. The correct length is 12. So the correct answer should be 12, but no option has 12. Options are 15, 17, 13, 11. 13 is closest but incorrect. So the code outputs 12, but no option matches. So the question has a bug. We must fix options or correct_answer. Let's check the code: echo strlen(trim($text)); The trimmed string is "Hello World!" length 12. So correct_answer should be none of these. So fix options: change 13 to 12 and correct_answer to C. Or change correct_answer to none. Let's fix 13 to "12" and correct_answer to ["C"].
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    trimmed string length = 12 [OK]
Hint: Count characters after removing spaces at ends with trim() [OK]
Common Mistakes:
  • Counting spaces inside the string
  • Forgetting trim() removes spaces at both ends
  • Confusing strlen() output with original string length
4. Identify the error in the following PHP code that tries to remove trailing spaces:
$str = "Test string   ";
$newStr = ltrim($str);
echo "'$newStr'";
medium
A. ltrim() removes spaces from the left, not the right
B. Missing semicolon after echo statement
C. Using single quotes inside echo causes error
D. Variable $newStr is not defined

Solution

  1. Step 1: Understand what ltrim() does

    ltrim() removes spaces from the left side of the string, but the spaces are on the right side.
  2. Step 2: Check code for other errors

    Semicolons are correct, single quotes inside echo are valid, and $newStr is defined properly.
  3. Final Answer:

    ltrim() removes spaces from the left, not the right -> Option A
  4. Quick Check:

    Use rtrim() to remove right side spaces [OK]
Hint: Use rtrim() for right side spaces, ltrim() for left [OK]
Common Mistakes:
  • Using ltrim() to remove right side spaces
  • Thinking single quotes cause errors in echo
  • Assuming variable is undefined without reason
5. You have a string $data = "\t Example Text \t" with tabs, newlines, and spaces around it. Which PHP function call will remove all these whitespace characters from both ends?
hard
A. trim($data, " \t\n")
B. ltrim($data, " \t")
C. rtrim($data)
D. trim($data)

Solution

  1. Step 1: Understand default trim() behavior

    By default, trim() removes spaces, tabs, newlines, carriage returns, and some other whitespace characters from both ends.
  2. Step 2: Check the character mask in options

    trim($data, " \t\n") explicitly specifies space, tab, and newline characters to remove, but the default trim() already removes these and more, so trim($data) is sufficient and preferred.
  3. Step 3: Analyze other options

    rtrim() and ltrim() remove only one side, so they won't clean both ends.
  4. Final Answer:

    trim($data) -> Option D
  5. Quick Check:

    Use trim() to remove all whitespace from both ends [OK]
Hint: Use trim() to remove all whitespace including tabs and newlines [OK]
Common Mistakes:
  • Using ltrim() or rtrim() when both ends need trimming
  • Assuming trim() removes only spaces, not tabs/newlines
  • Not specifying characters when needed for custom trimming
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard