PHP String Exercises : Change the case of letters or words
1. Transform String Case Conversions
Write a PHP script to : -
a) transform a string all uppercase letters.
b) transform a string all lowercase letters.
c) make a string's first character uppercase.
d) make a string's first character of all the words uppercase.
Visual Presentation:

Sample Solution:
PHP Code:
<?php
// Convert all characters to uppercase
print(strtoupper("the quick brown fox jumps over the lazy dog."))."\n";
// Convert all characters to lowercase
print(strtolower("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"))."\n";
// Make the first character uppercase
print(ucfirst("the quick brown fox jumps over the lazy dog."))."\n";
// Make the first character of each word uppercase
print(ucwords("the quick brown fox jumps over the lazy dog."))."\n";
?>
Output:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG. the quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog. The Quick Brown Fox Jumps Over The Lazy Dog.
Explanation:
In the exercise above,
- print(strtoupper("the quick brown fox jumps over the lazy dog.")): Converts all characters in the string to uppercase.
- print(strtolower("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")): Converts all characters in the string to lowercase.
- print(ucfirst("the quick brown fox jumps over the lazy dog.")): Makes the first character of the string uppercase.
- print(ucwords("the quick brown fox jumps over the lazy dog.")): Makes the first character of each word in the string uppercase.
Flowchart :

For more Practice: Solve these Related Problems:
- Write a PHP function to convert a given string to uppercase without using strtoupper(), utilizing ASCII values instead.
- Write a PHP script to convert a mixed-case string to lowercase by iterating over each character and adjusting its ASCII code.
- Write a PHP function that takes a string and makes only its first character uppercase, handling multibyte strings properly.
- Write a PHP program to transform each word’s first letter to uppercase by splitting the string, processing each word, and then rejoining them.
Go to:
PREV : PHP String Exercises Home.
NEXT : Split String Into Time Format.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.