Text does not always look the same. One word may appear in lowercase, another in uppercase. You want both to match. That is the problem. You cannot match words if the case does not match. PHP adds strtolower Function to fix that.
Table of Content
You can change every letter to lowercase. Let’s see how.
Understand the strtolower Function in PHP
The PHP strtolower()
function turns every uppercase letter in a string into lowercase. It does not touch numbers or symbols. The result is a clean version of the original.
Syntax:
strtolower($text)
You give it one string. It gives back the same string but in lowercase.
The function reads each character. It checks if the character is between A and Z. If it finds one, it switches it to lowercase.
It skips numbers, symbols, and anything that does not fall in that range. The original string stays the same. PHP gives back a new one.
Use Cases of strtolower
Function in PHP
People type the same word in different ways. One may type Email
, another may type EMAIL
. You do not want that to break your check. Use strtolower()
on both. You get the same case every time.
For example:
$input1 = 'Email';
$input2 = 'EMAIL';
if (strtolower($input1) === strtolower($input2)) {
echo 'Texts are matched';
} else {
echo 'They do not match';
}
The output:
Texts are matched
You clean the input before you store it. One person types Kenya
, another types kenya
. You store both as kenya
. That avoids extra work later.
$userInput = 'Kenya';
$cleaned = strtolower($userInput);
// Store $cleaned in database
// Saved value: 'kenya'
Here, store value of $cleaned
in database. It will save the value kenya
.
Usernames and URLs must follow a rule. They stay in lowercase. You run strtolower()
before you save them. That avoids mismatch and errors later.
$username = 'JohnSmith';
$url = '/Profile/JohnSmith';
$cleanUsername = strtolower($username);
$cleanUrl = strtolower($url);
echo $cleanUsername;
echo $cleanUrl;
Here is the output:
johnsmith
/profile/johnsmith
Let’s see more examples in the section below.
Examples
Clean up user tags:
Here, you clean the list before you store it. That helps you match and search later.
$tags = ['PHP', 'Web', 'Code'];
$clean = array_map('strtolower', $tags);
print_r($clean);
The output:
Array
(
[0] => php
[1] => web
[2] => code
)
Sort without case problems:
You force all words to lowercase before you sort them. That keeps the order fair.
$words = ['Zebra', 'apple', 'Orange'];
usort($words, fn($a, $b) => strcmp(strtolower($a), strtolower($b)));
The output:
Array
(
[0] => apple
[1] => Orange
[2] => Zebra
)
Match codes no matter the case:
Here, you change the input to lowercase before you check. That allows WIN50
, Win50
, or win50
to match.
$userCode = 'WIN50';
$validCodes = ['win50', 'deal30'];
if (in_array(strtolower($userCode), $validCodes)) {
echo 'Valid code';
}
Output:
Valid code
Wrapping Up
In this article you learned what strtolower()
does and where it fits. You saw how to use it in real checks and sorting.
Here is a quick recap:
strtolower()
function converts a string to all lowercase letters- It skips symbols and numbers.
- It helps you clean input and match text.
- You can use it for tags, usernames, and promo codes.
Thank you for reading. Click here to see more PHP tutorials.
FAQs
Does strtolower() affect numbers?
How to make the first character lowercase only?
$text = 'Hello';
$text[0] = strtolower($text[0]);
That changes only the first character.