PHP How to Convert String to Lowercase
strtolower() to convert a string to lowercase, like strtolower('Hello') which returns 'hello'.Examples
How to Think About It
Algorithm
Code
<?php
$input = "Hello World!";
$lowercase = strtolower($input);
echo $lowercase;
?>Dry Run
Let's trace converting 'Hello World!' to lowercase using strtolower()
Input string
The input string is 'Hello World!'
Convert to lowercase
strtolower('Hello World!') changes all uppercase letters to lowercase
Output result
The output is 'hello world!'
| Original | Lowercase |
|---|---|
| Hello World! | hello world! |
Why This Works
Step 1: Function Purpose
The strtolower() function changes all uppercase letters in a string to lowercase.
Step 2: Input Handling
It processes each character and converts only uppercase letters, leaving numbers and symbols unchanged.
Step 3: Output
It returns a new string with all letters in lowercase, which you can print or use further.
Alternative Approaches
<?php $input = "Äpfel"; $lowercase = mb_strtolower($input, 'UTF-8'); echo $lowercase; ?>
Complexity: O(n) time, O(n) space
Time Complexity
The function processes each character once, so the time grows linearly with the string length.
Space Complexity
A new string is created for the lowercase result, so space also grows linearly with input size.
Which Approach is Fastest?
strtolower() is fastest for ASCII strings; mb_strtolower() is needed for UTF-8 but slightly slower due to extra encoding handling.
| Approach | Time | Space | Best For |
|---|---|---|---|
| strtolower() | O(n) | O(n) | Simple ASCII strings |
| mb_strtolower() | O(n) | O(n) | UTF-8 strings with special characters |
strtolower() for simple ASCII strings and mb_strtolower() for UTF-8 encoded strings with special characters.mb_strtolower() when working with UTF-8 strings can cause incorrect lowercase conversion of special characters.