What is PHP strtolower() Function?
If you want to convert all the uppercase English alphabet in a string to lowercase, use PHP strtolower() function. All the numbers, symbols and special characters remain unchanged. The function can only operate on ASCII English alphabet. The name “strtolower” denotes “String to Lower(case)”.
Now, you may ask why it works only on ASCII encoding system and why not on other characters of different encoding systems? The answer is that the PHP strtolower() function treats each character as 1 byte and ASCII uses 1 byte to represent one character. So, the function can’t operate on multi-byte characters like Japanese alphabet. Also, the function doesn’t have option to specify character encoding system in its parameter so that it can handle multi-byte strings.
There are 256 ASCII characters (0-255) among which 32-126 are printable characters (letters, numbers, symbols, and punctuation marks). These 0-255 numbers are code points/codepoints/code positions. In ASCII encoding system, the code point 65 indicates English character “A” whose binary is 01000001, and the code point 90 indicates “Z”. Similarly, code point 97 for “a” and 122 for “z”.
Syntax:
strtolower(string)
Parameters:
The Function has 1 required parameter –
string (Required): It is a string whose English alphabet we want to convert to lowercase.
Return Values:
- The function returns a string with all lowercase English alphabet. Check example 1.
- The function returns “Uncaught ArgumentCountError” if you keep the parameter empty. Check example 2.
Examples:
Example 1: Making string lowercase-
<?php
echo strtolower("Learn Web development at Schools of Web (SOW).");
?>
Output:
learn web development at schools of web (sow).
Explanation:
The function makes all the characters to uppercase in the string to the lowercase.
Example 2: With no parameter-
<?php
echo strtolower();
?>
Output:
Fatal error: Uncaught ArgumentCountError: strtolower() expects exactly 1 argument, 0 given in D:\xampp\htdocs\strtolower.php:2 Stack trace: #0 D:\xampp\htdocs\strtolower.php(2): strtolower() #1 {main} thrown in D:\xampp\htdocs\strtolower.php on line 2
Explanation:
Trying to running strtolower() function without any parameter displays error.
Example 3: strtolower() function can work on binary data-
<?php
$string = "Check Course: ‘\x50\x48\x70 for Beginners’";
$convertedString = strtolower($string);
echo "Original String: ". $string . "<br /> Converted String: " . $convertedString;
?>
Output:
Original String: Check Course: ‘PHp for Beginners’
Converted String: check course: ‘php for beginners’
Explanation:
\x50 is a hexadecimal which is equivalent to uppercase “P”. Similarly, hexadecimal \x70 is lowercase “p” and hexadecimal \x748is uppercase “H”. the strtolower() function converts thee hexadecimal to their lowercase.
Example 4: Making NULL lowercase-
<?php
$string = NULL;
if (strtolower($string) != FALSE) {
// Continue coding.
}else{
echo "ERROR Occurred - NULL string.";
}
?>
Output:
ERROR Occurred – NULL string.
Explanation:
As using NULL as parameter in the strtolower() function returns FALSE, it is safe to make sure that the function haven’t returned FALSE.
Practical Usages of strtolower() Function:
There are many practical usages of this function.
- Every website contains a search feature. Users can search their necessary information whose case you can’t control. In that case, they won’t find the information because of the case mismatching. To solve this, you can convert both user search term and website stored information as lowercase by this function. Have a look at the code snippet.
ll<?php $searchString = strtolower($_POST["searchString"]); $storedString = strtolower($storedString); if(FALSE != strpos($storedString, $searchString)){ // send back the matching content } ?>
Note, here strpos() function returns FALSE if the search term doesn’t exist in the website.
- Sometimes you may need to display a string in all lowercase. For this, strtolower() is really a very useful function.
- You may also want to store content of a specific column in a database table in all lowercase for the data consistency. In this case, before storing the data in the table, convert it to lowercase.
- To ensure that a user always uses lowercase username and doesn’t mess with login regarding case, you can use this function to convert username to lowercase. Have a look at the code snippet-
<?php $Username = strtolower($_POST["Username"]); mysqli_query($connection, "INSERT INTO `login` (`Username`, ...) VALUES ('$Username', ...)"); ?>
- Users may use uppercase in their email address when they register. Email should be lowercase. So, you can convert user supplied email to lowercase with this function. Have a look at the code snippet-
<?php $Email = strtolower($_POST["Email"]); mysqli_query($connection, "INSERT INTO `users` (`Email`, ...) VALUES ('$Email', ...)"); ?>
- Users may use uppercase in URL when they add their website URL in the form. In standard practice, we don’t use uppercase in URL. So, we can use strtolower() function to convert the URL to lowercase. Have a look at the code snippet-
<?php $URL = strtolower($_POST["URL"]); mysqli_query($connection, "INSERT INTO `users` (`URL`, ...) VALUES ('$URL', ...)"); ?>
Notes on strtolower() Function:
- It is a binary-safe function. This function considers its input as byte, it handles binary data (i.e. non-ASCII bytes, UTF-8) correctly. Check example 3.
- As this function can’t handle multi-byte characters. It can’t convert non-English alphabet characters to lowercase, use mb_strtolower() function instead.
- Prior to PHP version 8.2.0, PHP uses locale set with setlocale() for the case conversion. But, now, it exclusively depends on ASCII encoding system for this conversion.
Best practices on strtolower() Function:
Save your memory resource by avoid frequent calling this function when the string is already in lowercase.
Caution:
- Be careful when converting multi-byte non-ASCII characters. It wouldn’t work with these characters as expected.
- Check whether the function FALSE before working with the resultant string. If you’ve provided a NULL value accidentally, the function returns FALSE. Check example 4.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP strtolower() Function
It’s a great function to control strings consistency in any web project. Managing case effectively is important in any programming language. The built-in string function strtolower() is such a function in PHP.