Case Conversion in PHP: Uppercase and Lowercase
Case conversion in PHP refers to changing the letters of a string from uppercase to lowercase or vice versa. PHP offers several built-in functions that make this process straightforward. They are:
- strtolower() Function
- strtoupper() Function
- ucfirst() Function
- ucwords() Function
Lowercase Conversion with PHP strtolower() Function
PHP provides a built-in function named strtolower() that is used to convert all alphabetic characters in a string to lowercase. You can use the strtolower() function in the following cases:
- To make comparisons case-insensitive.
- To store consistent data (like emails, usernames).
- For formatting URLs or SEO slugs.
- When accepting user input in various cases.
The general syntax to define strtolower() function in PHP is as follows:
strtolower(string $string): string
This function accepts an input string $string as argument that you want to convert to lowercase.
Return Value
The strtolower() function returns the lowercase version of the input string. So, you have to store it using a variable.
Example 1:
<?php
$text = "SCIENTECH EASY";
// Converting characters of a string from uppercase to lowercase.
$lowercaseText = strtolower($text);
echo $lowercaseText;
?>
Output: scientech easy
Example 2: Case-Insensitive Comparison
<?php
$input = "Admin";
$storedUsername = "admin";
if (strtolower($input) === strtolower($storedUsername)) {
echo "User authenticated.";
} else {
echo "Invalid username.";
}
?>
Output: User authenticated.
Upper Case Conversion using PHP strtoupper() Function
The strtoupper() function in PHP is a built-in function that is used to convert all alphabetic characters of a string into uppercase. The general syntax to define strtoupper() function is as follows:
strtoupper(string $string): string
In the above syntax, the parameter $string represents the input string that you want to convert to uppercase.
Return Value
The strtoupper() function returns the uppercase version of the input string.
Example 3:
<?php
$text = "Scientech Easy";
// Converting the characters of string from mixed to uppercase.
$uppercaseText = strtoupper($text);
echo $uppercaseText;
?>
Output: SCIENTECH EASY
Capitalizing the First Letter using ucfirst() Function
The ucfirst() function in PHP is a built-in function that is used to capitalize the first character of a string. The general syntax to define the ucfirst() function is as follows:
ucfirst(string $string): string
Example 4:
<?php
$text = "hello world!";
// Capitalizing the first letter of a string using ucfirst() function.
$capitalized = ucfirst($text);
echo $capitalized;
?>
Output: Hello world!
Capitalizing the First Letter of Each Word using ucwords() Function
The ucwords() function is a built-in function provided by PHP that is used to capitalize the first letter of each word in a string. The basic form of this function is as:
ucwords(string $string): string
Example 5:
<?php
$text = "introduction to php programming!";
// Capitalizing the first letter of each word.
$capitalizedWords = ucwords($text);
echo $capitalizedWords;
?>
Output: Introduction To Php Programming!
In this tutorial, we have learned about case conversion in PHP with the help of various examples. I hope that you will have understood that how to convert characters of a string into upper or lower case. Here’s a quick recap of PHP’s case conversion functions:
- strtolower(): Converts all characters to lowercase.
- strtoupper(): Converts all characters to uppercase.
- ucfirst(): Capitalizes the first character of a string.
- ucwords(): Capitalizes the first character of each word.