0
0
PhpHow-ToBeginner · 2 min read

PHP How to Convert String to Lowercase

Use the built-in PHP function strtolower() to convert a string to lowercase, like strtolower('Hello') which returns 'hello'.
📋

Examples

InputHELLO
Outputhello
InputPhp Is Fun!
Outputphp is fun!
Input123 ABC xyz!
Output123 abc xyz!
🧠

How to Think About It

To convert a string to lowercase in PHP, think about changing every uppercase letter to its lowercase form while leaving other characters unchanged. PHP provides a simple function that does this for you, so you just need to pass your string to it.
📐

Algorithm

1
Get the input string.
2
Use the built-in function to convert all uppercase letters to lowercase.
3
Return or print the converted lowercase string.
💻

Code

php
<?php
$input = "Hello World!";
$lowercase = strtolower($input);
echo $lowercase;
?>
Output
hello world!
🔍

Dry Run

Let's trace converting 'Hello World!' to lowercase using strtolower()

1

Input string

The input string is 'Hello World!'

2

Convert to lowercase

strtolower('Hello World!') changes all uppercase letters to lowercase

3

Output result

The output is 'hello world!'

OriginalLowercase
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

mb_strtolower()
php
<?php
$input = "Äpfel";
$lowercase = mb_strtolower($input, 'UTF-8');
echo $lowercase;
?>
Use <code>mb_strtolower()</code> for multibyte (UTF-8) strings to correctly convert accented or non-ASCII characters.

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.

ApproachTimeSpaceBest For
strtolower()O(n)O(n)Simple ASCII strings
mb_strtolower()O(n)O(n)UTF-8 strings with special characters
💡
Use strtolower() for simple ASCII strings and mb_strtolower() for UTF-8 encoded strings with special characters.
⚠️
Forgetting to use mb_strtolower() when working with UTF-8 strings can cause incorrect lowercase conversion of special characters.