Introduction
PHP provides several functions to change the capitalization of text and remove unwanted whitespace. These functions are useful when formatting employee names, standardizing product information, cleaning customer input, and preparing text for display. In this chapter, you will understand the concepts PHP String Case Conversion and Trimming practice questions with solutions using strtoupper(), strtolower(), ucfirst(), ucwords(), trim(), ltrim(), and rtrim() through practical PHP questions with solutions.
Question 1: Convert a String to Uppercase Using strtoupper()
Problem Statement
Write a PHP program to convert the string "welcome to php programming" into uppercase letters.
PHP Solution
<?php
$text = "welcome to php programming";
$result = strtoupper($text);
echo "Original Text: " . $text . "<br>";
echo "Uppercase Text: " . $result;
?>
Sample Output
Original Text: welcome to php programming
Uppercase Text: WELCOME TO PHP PROGRAMMING
Explanation
The strtoupper() function converts lowercase English letters in a string to uppercase.
In this example, all lowercase letters are converted to capital letters, while spaces remain unchanged.
This function is useful when displaying headings, product codes, or other text that needs consistent capitalization.
Concepts Covered
strtoupper()function- Uppercase conversion
- String variables
- Displaying formatted text
Question 2: Convert a String to Lowercase Using strtolower()
Problem Statement
A company stores customer email addresses with inconsistent capitalization. Write a PHP program to convert "RAHUL@EXAMPLE.COM" into lowercase.
PHP Solution
<?php
$email = "RAHUL@EXAMPLE.COM";
$formattedEmail = strtolower($email);
echo "Original Email: " . $email . "<br>";
echo "Lowercase Email: " . $formattedEmail;
?>
Sample Output
Original Email: RAHUL@EXAMPLE.COM
Lowercase Email: rahul@example.com
Explanation
The strtolower() function converts uppercase English letters to lowercase.
Here, the entire email address is converted to lowercase, making its capitalization consistent.
Lowercasing an email address can help with consistent display and application-specific comparisons. However, email address handling should follow the rules of the system receiving the address; lowercasing does not verify that an email address exists.
Concepts Covered
strtolower()function- Lowercase conversion
- Email formatting
- Consistent text representation
Question 3: Capitalize the First Letter Using ucfirst()
Problem Statement
Write a PHP program to capitalize the first letter of the string "welcome to vsit" while leaving the remaining letters unchanged.
PHP Solution
<?php
$text = "welcome to vsit";
$result = ucfirst($text);
echo "Original Text: " . $text . "<br>";
echo "Formatted Text: " . $result;
?>
Sample Output
Original Text: welcome to vsit
Formatted Text: Welcome to vsit
Explanation
The ucfirst() function converts the first character of a string to uppercase.
It does not automatically convert the other characters to lowercase.
For example, "wELCOME TO VSIT" would become "WELCOME TO VSIT" because the first character is already uppercase and the remaining characters are unchanged.
Note: ucfirst() is not fully Unicode-aware for multibyte characters. For UTF-8 text that includes Hindi or other non-ASCII characters, use suitable multibyte string functions when available.
Concepts Covered
ucfirst()function- Capitalizing the first character
- String formatting
- Text presentation
Question 4: Capitalize the First Letter of Every Word Using ucwords()
Problem Statement
A student enters their name as "rahul kumar sharma". Write a PHP program to capitalize the first letter of every word.
PHP Solution
<?php
$studentName = "rahul kumar sharma";
$formattedName = ucwords($studentName);
echo "Original Name: " . $studentName . "<br>";
echo "Formatted Name: " . $formattedName;
?>
Sample Output
Original Name: rahul kumar sharma
Formatted Name: Rahul Kumar Sharma
Explanation
The ucwords() function converts the first character of each word to uppercase.
It is useful for formatting names, headings, and labels.
However, it does not convert every other letter to lowercase. If the input is "rAHUL kUMAR", the result will not automatically become "Rahul Kumar".
Concepts Covered
ucwords()function- Capitalizing words
- Formatting student names
- String manipulation
Question 5: Remove Spaces from Both Ends Using trim()
Problem Statement
A registration form receives the name " Amit Sharma " with extra spaces at the beginning and end. Write a PHP program to remove those spaces.
PHP Solution
<?php
$name = " Amit Sharma ";
$cleanName = trim($name);
echo "Original Length: " . strlen($name) . "<br>";
echo "Clean Name: " . $cleanName . "<br>";
echo "Clean Length: " . strlen($cleanName);
?>
Sample Output
Original Length: 17
Clean Name: Amit Sharma
Clean Length: 11
Explanation
The trim() function removes whitespace from the beginning and end of a string.
The original string contains three spaces before the name and three after it. After removing these six spaces, the name contains 11 bytes because the English letters and the internal space each occupy one byte.
The space between "Amit" and "Sharma" remains unchanged.
This function is useful for cleaning text received from forms.
Concepts Covered
trim()function in PHP- Removing leading and trailing whitespace
strlen()function- Cleaning form input
Question 6: Remove Spaces Only from the Beginning Using ltrim()
Problem Statement
A company receives a product description with unwanted spaces at the beginning. Write a PHP program to remove the leading spaces from " Premium Laptop" while keeping the text and any trailing spaces unchanged.
PHP Solution
<?php
$product = " Premium Laptop ";
$result = ltrim($product);
echo "Original Length: " . strlen($product) . "<br>";
echo "After ltrim(): " . $result . "<br>";
echo "New Length: " . strlen($result);
?>
Sample Output
Original Length: 23
After ltrim(): Premium Laptop
New Length: 18
Explanation
The ltrim() function removes whitespace from the beginning, or left side, of a string.
It does not remove the trailing spaces at the end.
In this example, five leading spaces are removed, while the three trailing spaces remain.
The original string has 23 bytes, and the resulting string has 18 bytes.
Concepts Covered
ltrim()function- Removing leading whitespace
- Preserving trailing whitespace
- String length calculation
Question 7: Remove Spaces Only from the End Using rtrim()
Problem Statement
Write a PHP program to remove trailing spaces from the string "VSIT Coding Institute " while preserving any spaces at the beginning.
PHP Solution
<?php
$institute = "VSIT Coding Institute ";
$result = rtrim($institute);
echo "Original Length: " . strlen($institute) . "<br>";
echo "Clean Text: " . $result . "<br>";
echo "New Length: " . strlen($result);
?>
Sample Output
Original Length: 26
Clean Text: VSIT Coding Institute
New Length: 21
Explanation
The rtrim() function removes whitespace from the end, or right side, of a string.
The original text contains five trailing spaces. After removing them, the string length decreases from 26 to 21 bytes.
Unlike trim(), this function leaves leading spaces untouched.
Concepts Covered
rtrim()function- Removing trailing whitespace
- String length
- Text cleanup
Question 8: Convert Text to Lowercase and Capitalize Every Word
Problem Statement
A student enters their name as "rAHUL kUMAR sHARMA". Write a PHP program to convert the entire name to lowercase first and then capitalize the first letter of each word.
PHP Solution
<?php
$name = "rAHUL kUMAR sHARMA";
$lowercaseName = strtolower($name);
$formattedName = ucwords($lowercaseName);
echo "Original Name: " . $name . "<br>";
echo "Formatted Name: " . $formattedName;
?>
Sample Output
Original Name: rAHUL kUMAR sHARMA
Formatted Name: Rahul Kumar Sharma
Explanation
The program uses two functions in sequence.
First, strtolower() converts all uppercase English letters to lowercase. The result is:
rahul kumar sharma
Next, ucwords() capitalizes the first letter of each word.
The final result is "Rahul Kumar Sharma".
Combining these functions is useful when user input contains inconsistent capitalization.
Concepts Covered
strtolower()functionucwords()function- Combining string functions
- Standardizing names
Question 9: Remove Extra Spaces and Convert a Name to Proper Case
Problem Statement
A company stores an employee name as " aMIT sHARMA ". Write a PHP program to remove the extra spaces at the beginning and end, convert the name to lowercase, and capitalize the first letter of each word.
PHP Solution
<?php
$employeeName = " aMIT sHARMA ";
$cleanName = trim($employeeName);
$lowercaseName = strtolower($cleanName);
$formattedName = ucwords($lowercaseName);
echo "Original Name: [" . $employeeName . "]<br>";
echo "Formatted Name: [" . $formattedName . "]";
?>
Sample Output
Original Name: [ aMIT sHARMA ]
Formatted Name: [Amit Sharma]
Explanation
The program performs three operations:
trim()removes whitespace from the beginning and end.strtolower()converts the remaining English letters to lowercase.ucwords()capitalizes the first letter of each word.
The spaces between "aMIT" and "sHARMA" are preserved. The trim() function does not collapse multiple internal spaces into one.
This distinction is important when cleaning form data.
Concepts Covered
trim()function in PHPstrtolower()functionucwords()function- Combining string functions
- Employee data formatting
Question 10: Remove Specific Characters from a Product Code
Problem Statement
A company stores a product code as "***PRD2026***". Write a PHP program to remove the asterisks from the beginning and end of the code using trim().
PHP Solution
<?php
$productCode = "***PRD2026***";
$cleanCode = trim($productCode, "*");
echo "Original Code: " . $productCode . "<br>";
echo "Clean Code: " . $cleanCode;
?>
Sample Output
Original Code: ***PRD2026***
Clean Code: PRD2026
Explanation
The second argument of trim() specifies which characters to remove from both ends of the string.
Here, "*" tells PHP to remove asterisks from the beginning and end.
The characters inside the string are preserved. The function removes matching characters only from the ends, not from the middle.
For example, trim("***PRD*2026***", "*") returns "PRD*2026" because the internal asterisk remains.
Concepts Covered
trim()function in PHP with a character mask- Removing specific characters
- Cleaning product codes
- String manipulation
Key Takeaways
strtoupper()converts lowercase English letters to uppercase.strtolower()converts uppercase English letters to lowercase.ucfirst()capitalizes the first character of a string.ucwords()capitalizes the first character of each word.trim()removes whitespace from both ends of a string.ltrim()removes whitespace from the beginning of a string.rtrim()removes whitespace from the end of a string.trim()can also remove specified characters from both ends.- Combining string functions helps standardize names and clean user input.
- The standard capitalization functions are not fully Unicode-aware for multibyte text.
Frequently Asked Questions (FAQs)
1. What is string case conversion in PHP?
String case conversion means changing the capitalization of letters. PHP provides functions such as strtoupper() and strtolower() to convert English letters to uppercase or lowercase.
2. What is the difference between strtoupper() and strtolower()?
strtoupper() converts lowercase English letters to uppercase, while strtolower() converts uppercase English letters to lowercase.
<?php
echo strtoupper("hello") . "<br>";
echo strtolower("HELLO");
?>
Output:
HELLO
hello
3. What does ucfirst() do in PHP?
The ucfirst() function converts the first character of a string to uppercase without automatically changing the remaining characters.
<?php
echo ucfirst("welcome");
?>
Output:
Welcome
4. What does ucwords() do in PHP?
The ucwords() function converts the first character of each word to uppercase.
<?php
echo ucwords("learn php programming");
?>
Output:
Learn Php Programming
Notice that it does not automatically preserve special capitalization such as PHP. To retain "PHP" in uppercase, you may need additional formatting logic.
5. What is the difference between trim(), ltrim(), and rtrim()?
trim()removes whitespace from both ends.ltrim()removes whitespace from the beginning.rtrim()removes whitespace from the end.
None of these functions removes internal spaces by default.
6. Does trim() remove spaces from the middle of a string?
No. It removes whitespace only from the beginning and end.
For example, trim(" Hello World ") returns "Hello World". The three spaces between the words remain unchanged.
7. Can trim() remove characters other than spaces?
Yes. You can provide a character mask as the second argument.
<?php
echo trim("***PHP***", "*");
?>
Output:
PHP
8. How can you format a student’s name correctly in PHP?
For basic English names, combine strtolower() and ucwords().
<?php
$name = "rAHUL kUMAR";
echo ucwords(strtolower(trim($name)));
?>
Output:
Rahul Kumar
This example removes leading and trailing whitespace, converts the name to lowercase, and capitalizes each word. Real names may have special capitalization rules, so automatic formatting is not suitable for every name.
9. Why should you use trim() when processing form input?
Users may accidentally enter spaces before or after their names, email addresses, or other text. Applying trim() can remove this unwanted whitespace before further processing. It does not replace proper validation or security checks.
10. Do PHP case-conversion functions work correctly with Hindi text and emojis?
Not always. Functions such as strtoupper(), strtolower(), and ucfirst() have limitations with multibyte UTF-8 text. For Unicode-aware case conversion, consider the mbstring extension and functions such as mb_strtolower() and mb_strtoupper() when available. Emojis generally do not have uppercase or lowercase forms.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
