Case conversion functions in PHP - Time & Space Complexity
We want to understand how the time it takes to change text case grows as the text gets longer.
How does the program's work increase when we convert bigger strings?
Analyze the time complexity of the following code snippet.
$string = "Hello World!";
$lower = strtolower($string);
$upper = strtoupper($string);
// Convert each character to lowercase and uppercase
This code converts a string to all lowercase and all uppercase letters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The functions check each character in the string one by one.
- How many times: Once for each character in the string.
As the string gets longer, the work grows in a straight line with the number of characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: Doubling the string length roughly doubles the work.
Time Complexity: O(n)
This means the time to convert case grows directly with the string length.
[X] Wrong: "Changing case is instant no matter the string size."
[OK] Correct: Each character must be checked and changed, so longer strings take more time.
Understanding how simple string operations scale helps you explain efficiency clearly and confidently.
"What if we changed the string to an array of words and converted each word's case separately? How would the time complexity change?"
Practice
$text = "hello world";Solution
Step 1: Understand the function purpose
strtoupper()converts all characters in a string to uppercase letters.Step 2: Compare with other functions
strtolower()makes all letters lowercase,ucfirst()capitalizes only the first letter, anducwords()capitalizes the first letter of each word.Final Answer:
strtoupper() -> Option AQuick Check:
Uppercase function = strtoupper() [OK]
- Confusing strtoupper() with strtolower()
- Using ucfirst() to uppercase whole string
- Using ucwords() for full uppercase
$str = "HELLO";Solution
Step 1: Identify correct function call syntax
In PHP, functions are called with parentheses enclosing arguments, likefunctionName(argument);.Step 2: Check each option
strtolower($str); uses correct syntax:strtolower($str);. Others have syntax errors or invalid usage.Final Answer:
strtolower($str); -> Option CQuick Check:
Function call syntax = strtolower($str); [OK]
- Missing parentheses in function call
- Using arrow or braces incorrectly
- Confusing function call with variable access
$name = "john doe";
echo ucfirst($name);
Solution
Step 1: Understand ucfirst() behavior
ucfirst()capitalizes only the first character of the entire string, leaving the rest unchanged.Step 2: Apply to given string
Input is "john doe". Afterucfirst(), it becomes "John doe" (only first letter capitalized).Final Answer:
John doe -> Option AQuick Check:
ucfirst("john doe") = "John doe" [OK]
- Expecting all words capitalized
- Confusing ucfirst() with ucwords()
- Assuming it converts whole string to uppercase
$text = "hello world";
echo ucfirst($text);
What is the correct fix?
Solution
Step 1: Identify function behavior
ucfirst()capitalizes only the first letter of the entire string, not each word.Step 2: Choose function to capitalize each word
ucwords()capitalizes the first letter of every word in the string.Final Answer:
Replace ucfirst() with ucwords() -> Option BQuick Check:
Capitalize each word = ucwords() [OK]
- Using ucfirst() instead of ucwords()
- Using strtoupper() which uppercases all letters
- Thinking code is already correct
$input = "hElLo WoRLd";. You want to convert it so that only the first letter of each word is uppercase and the rest are lowercase. Which PHP code achieves this?Solution
Step 1: Normalize string to lowercase
Usestrtolower()to convert all letters to lowercase first, so mixed case is fixed.Step 2: Capitalize first letter of each word
Applyucwords()to capitalize the first letter of every word in the now lowercase string.Final Answer:
$output = ucwords(strtolower($input)); -> Option DQuick Check:
Lowercase then ucwords() = desired format [OK]
- Applying ucwords() before strtolower()
- Using strtoupper() which uppercases all letters
- Using ucfirst() which only capitalizes first letter of string
Ready to prove it?
Pass the test and this pattern moves unknown
