strtolower() / strtoupper() / mb_strtolower() Since: PHP 4(2000)
Converts the case of letters in a string. Use mb_strtolower() or mb_strtoupper() when the string contains multibyte characters.
Syntax
// Converts all letters to lowercase. strtolower($string); // Converts all letters to uppercase. strtoupper($string); // Capitalizes only the first character of the string. ucfirst($string); // Capitalizes the first letter of each word. ucwords($string, $separators); // Converts to lowercase with multibyte character support. mb_strtolower($string, $encoding); // Converts to uppercase with multibyte character support. mb_strtoupper($string, $encoding);
Function List
| Function | Description |
|---|---|
| strtolower($string) | Converts all alphabetic characters in a string to lowercase. |
| strtoupper($string) | Converts all alphabetic characters in a string to uppercase. |
| ucfirst($string) | Converts only the first character of a string to uppercase. |
| ucwords($string, $separators) | Capitalizes the first letter of each word. You can specify delimiter characters via the second argument; if omitted, spaces, tabs, and newlines are used as delimiters. |
| mb_strtolower($string, $encoding) | Converts a string to lowercase with multibyte character support. |
| mb_strtoupper($string, $encoding) | Converts a string to uppercase with multibyte character support. |
Return Value
Returns the converted string. The original string is not modified.
Sample Code
<?php
// Converts all letters to lowercase.
echo strtolower("Hello WORLD"); // Outputs "hello world".
// Converts all letters to uppercase.
echo strtoupper("Hello World"); // Outputs "HELLO WORLD".
// Capitalizes only the first character.
echo ucfirst("hello world"); // Outputs "Hello world".
// Capitalizes the first letter of each word.
echo ucwords("hello world php"); // Outputs "Hello World Php".
// Specifies a custom delimiter to split words.
echo ucwords("hello-world-php", "-"); // Outputs "Hello-World-Php".
// Useful for case-insensitive comparisons.
$input = "Tokyo";
if (strtolower($input) === "tokyo") {
echo "Matched."; // Compares strings without regard to case.
}
// Multibyte-aware conversion.
echo mb_strtoupper("straße", "UTF-8"); // Outputs "STRASSE". The German eszett is converted.
// Example: normalizing an email address.
$email = "User@Example.COM";
echo strtolower($email); // Outputs "user@example.com".
Overview
strtolower() and strtoupper() convert the case of all alphabetic characters in a string at once. They are commonly used for normalizing email addresses and URLs, and for performing case-insensitive comparisons.
ucfirst() is handy when you want to capitalize only the first letter of a sentence, while ucwords() is useful for converting text to title case. ucwords() accepts a second argument to specify custom delimiters, making it suitable for strings separated by hyphens, underscores, or other characters.
Use mb_strtolower() and mb_strtoupper() when you need to handle multibyte characters such as the German eszett. For string searching, strpos() is useful, and for case-insensitive replacement, str_ireplace() is a good choice.
If you find any errors or copyright issues, please contact us.