PHP strtolower Function: Convert Strings to Lowercase

PHP strtolower Function

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?

No. Numbers stay the same. Only letters change.

How to make the first character lowercase only?

Use this code:
$text = 'Hello';
$text[0] = strtolower($text[0]);
That changes only the first character.

What is the opposite of strtolower()?

Use strtoupper(). It changes every letter to uppercase.

Similar Reads

PHP Arithmetic Operators: Essential Guide

Perhaps you're new to PHP or sharpening your skills and looking to understand how numbers work within this language under…

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

PHP SimpleXML: Work with XML in Examples

PHP SimpleXML - invented for you to work with XML. Way leaner, quicker to get up. it's already integrated directly…

How to Insert Multiple Rows in MySQL with PHP?

Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…

PHP Logical Operators | Understanding the 6 Logical Operators

The PHP logical operators are operands that can be used to check two or more expressions together. For examples (…

What Is PHP Used For? Meaning of PHP Programming Language

When you first step into web development, one name seems to pop up everywhere: PHP. The PHP programming language has…

PHP Data Types: Understanding the 10 Primitive Types

The whole world of PHP programming revolves around data, be it numbers, text, or more complicated structures. Each of these…

PHP substr Function: How to Extract and Manipulate Strings

The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

PHP file_exists: Check File and Directory Existence

Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…

PHP $GLOBALS: Access Global Variables with Examples

When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…

Previous Article

Assignment Operator in JavaScript with Examples

Next Article

PHP trim(): How to remove Whitespace from String

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.