Table of Content
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.
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.
What is the opposite of strtolower()?
Similar Reads
The PHP array_find_key function finds a key in an array by a condition. It checks each element and returns the…
Understanding how to update data in PHP and MySQL is like editing a draft—it is all about tweaking the right…
The PHP array_key_exists function checks a given key in an array and tells if that key exists or not. Understand…
PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…
Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…
The PHP object is an instance from the PHP class or the main data structure that is already been created…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…
A PHP float is a kind of number that has a decimal point, like 2.45 or 0.11. It’s also called…
PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…
This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…