Trim functions in PHP - Time & Space Complexity
We want to understand how the time to run trim functions changes as the input string gets longer.
How does the length of the string affect the work done by trim functions?
Analyze the time complexity of the following code snippet.
$string = " Hello World ";
$trimmed = trim($string);
$leftTrimmed = ltrim($string);
$rightTrimmed = rtrim($string);
// Using trim functions to remove spaces from the string ends
This code removes spaces from the start and/or end of a string using PHP's trim functions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning characters from the start and/or end of the string to find spaces.
- How many times: At most once per character from each end until a non-space is found.
As the string gets longer, the trim functions check characters from the ends until they find a non-space.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows roughly in direct proportion to the length of the string.
Time Complexity: O(n)
This means the time to trim grows linearly with the string length.
[X] Wrong: "Trim functions always run in constant time no matter the string size."
[OK] Correct: The functions must check characters from the ends until they find a non-space, so longer strings can take more time.
Understanding how simple string operations scale helps you reason about performance in real projects and interviews.
"What if the string had no spaces at the ends? How would the time complexity change?"
Practice
trim() do to a string?Solution
Step 1: Understand the purpose of trim()
Thetrim()function removes unwanted whitespace characters from the start and end of a string.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.Final Answer:
Removes whitespace from both the beginning and end of the string -> Option BQuick Check:
trim() removes spaces at both ends [OK]
- Thinking trim() removes spaces inside the string
- Confusing trim() with strtoupper()
- Assuming trim() removes only from one side
Solution
Step 1: Identify function for left-side trimming
The functionltrim()removes whitespace from the left (start) side of a string.Step 2: Check other options
rtrim()removes from right side,trim()removes both sides, andstrip()is not a valid PHP function.Final Answer:
ltrim($str) -> Option AQuick Check:
ltrim() trims left side only [OK]
- Using rtrim() to trim left side
- Using trim() when only left side is needed
- Using non-existent function strip()
$text = " Hello World! "; echo strlen(trim($text));
Solution
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.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"].Final Answer:
12 -> Option CQuick Check:
trimmed string length = 12 [OK]
- Counting spaces inside the string
- Forgetting trim() removes spaces at both ends
- Confusing strlen() output with original string length
$str = "Test string "; $newStr = ltrim($str); echo "'$newStr'";
Solution
Step 1: Understand what ltrim() does
ltrim() removes spaces from the left side of the string, but the spaces are on the right side.Step 2: Check code for other errors
Semicolons are correct, single quotes inside echo are valid, and $newStr is defined properly.Final Answer:
ltrim() removes spaces from the left, not the right -> Option AQuick Check:
Use rtrim() to remove right side spaces [OK]
- Using ltrim() to remove right side spaces
- Thinking single quotes cause errors in echo
- Assuming variable is undefined without reason
$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?Solution
Step 1: Understand default trim() behavior
By default,trim()removes spaces, tabs, newlines, carriage returns, and some other whitespace characters from both ends.Step 2: Check the character mask in options
trim($data, " \t\n") explicitly specifies space, tab, and newline characters to remove, but the defaulttrim()already removes these and more, sotrim($data)is sufficient and preferred.Step 3: Analyze other options
rtrim() and ltrim() remove only one side, so they won't clean both ends.Final Answer:
trim($data) -> Option DQuick Check:
Use trim() to remove all whitespace from both ends [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
