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 Associative Array: Optimal Data Organization

The PHP associative array is a structured collection wherein data elements are organized into lists or groups. Each element is…

PHP fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

PHP array_flip Function: How it Works with Examples

PHP array_flip swaps keys with values. It works well when you need values as keys in fast lookups. What is…

PHP Elvis Operator: How (?:) Works with Examples

The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…

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…

PHP IF Statement: A Guide to Conditional Logic

The IF statement in PHP helps to see if a condition is true or not. If it is true, it…

Understanding PHP Constants: A Simple Guide with Examples

In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…

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 (…

PHP $_COOKIE: Securely Store and Manage Data

Ever notice how some websites just seem to "know" you? That’s thanks to cookies! When you’re working with PHP, $_COOKIE becomes a…

Concatenating Strings in PHP: Tips and Examples

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and…

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.