How to Use strtolower in PHP: Convert Strings to Lowercase
Use the
strtolower function in PHP to convert all characters in a string to lowercase. Simply pass the string as an argument like strtolower('Hello'), which returns 'hello'.Syntax
The strtolower function takes one parameter, a string, and returns a new string with all alphabetic characters converted to lowercase.
- string $string: The input string you want to convert.
- Returns: A string with all uppercase letters changed to lowercase.
php
strtolower(string $string): string
Example
This example shows how to convert a mixed-case string to all lowercase using strtolower.
php
<?php
$original = "Hello World!";
$lowercase = strtolower($original);
echo $lowercase;
?>Output
hello world!
Common Pitfalls
One common mistake is expecting strtolower to change non-alphabetic characters or handle multibyte characters like accented letters correctly. It only affects ASCII letters A-Z. For multibyte strings, use mb_strtolower.
Also, strtolower does not change the original string but returns a new one, so you must assign or use the returned value.
php
<?php // Wrong: expecting original string to change $word = "HELLO"; strtolower($word); echo $word; // Outputs: HELLO // Right: assign the result $word = "HELLO"; $word = strtolower($word); echo $word; // Outputs: hello ?>
Output
HELLO
hello
Quick Reference
| Function | Description | Example | Output |
|---|---|---|---|
| strtolower | Converts ASCII letters to lowercase | strtolower('PHP') | 'php' |
| mb_strtolower | Converts multibyte strings to lowercase | mb_strtolower('ÄÖÜ') | 'äöü' |
Key Takeaways
Use strtolower to convert ASCII strings to lowercase in PHP.
Always assign the result of strtolower to a variable; it does not modify the original string.
For multibyte or accented characters, use mb_strtolower instead.
strtolower only affects alphabetic characters A-Z, leaving others unchanged.