PHP comes with a wealth of internally built functions, all ready for you to add to your codebase. The idea behind them is to save you time and avoid having to write your own version of similar tasks. Text processing, in particular strings, is a very common task you'll have to do when programming your next website. Converting a string to all lowercase is just one of those tasks that are good to understand and have under your belt. We'll explore how the PHP 'strtolower' function works all accompanied by code examples to show you the way.
What is the strtolower function in PHP
The strtolower function makes up a collection of PHP internal functions for text processing strings within your application. They allow you to manipulate strings in all sorts of ways. The function 'strtolower' stands for String to lower(case), which will take a given string and make all ASCII alphabetic characters lowercase. This function is a great way to control strings in your web application. Maybe you need to display strings or store strings in all lowercase. This function is also useful if you want to batch run a conversion of strings, maybe this on pre-existing data. Couple this by running your PHP script on the CLI, you could ensure your strings remain in a particular format.
You may consider changing a string to all lowercase for a number of reasons but the primary aim of your web application should be to achieve and maintain data consistency.
- When working with files and folders in a file system, as some operating systems are case-sensitive and therefore lowercasing file names and folders can help avoid future issues
- If you are storing data in a database, to help with data consistency and susiquent data retrieval (when querying the database), converting data to lowercase before storing can help avoid case-related issues
- If within your code logic, you are comparing strings, such as a user term versus a database stored value, ensure they are both lowercase first will make the comparisons simpler and more likely to match, especially true when there is a mismatch of upper and lowercase values
- Another real-world example is when creating website URLs within your application. The standard URL path name should be all lowercase, and therefore converting to lowercase will ensure correct routing within your application
- A very common use case for lowering strings to all lowercase is when handling user data. Email addresses by default (and by the governing standard) should be lowercase. Therefore ensuring email address is all lowercase before handling is a good idea
How does the strtolower function work?
The strtolower function works by accepting a string. The string can be all uppercase or a combination of lowercase and uppercase. It will convert all characters to lowercase and return the string. First appeared in PHP version 4 and is still available today in PHP 8+.
Let's take an example and write a PHP script to convert uppercase string to lowercase.
# Will convert to; hello world
strtolower('HELLO WORLD');
A mismatch of upper and lower case letters will still be converted to all to lowercase, in this following example.
# Will convert to; hello world
strtolower('hEllO wORlD');
All types of ASCII alphabetic characters are converted to lowercase, as shown in the following example.
# Will convert to; 123 abc £$%
strtolower('123 ABC £$%');
This function will also ignore any multibyte UTF-8 characters, therefore string can still contain them, simply, they'll not be returned by the function after conversion. If you require to convert non-ASCII characters, you should use the mb_strtolower function. Since PHP version 8.2, PHP does not depend on the server's set locale, settable with the 'setlocale' function as only ASCII characters are converted.
Failure to pass a string or passing null into the strtolower function will result in various PHP errors. The first error that could occur is, wrong argument count. This is because passing nothing to strtolower function isn't allowed, as you cannot string lower an empty space. The second error is a deprecated error where passing a null value to the strtolower function will trigger this error. Similar to not passing anything, passing null would do nothing and is a waste of computing time. Avoid passing nothing and null values to the strtolower function.
/* Will trigger "Fatal error: Uncaught ArgumentCountError: strtolower() expects
exactly 1 argument, 0 given"*/
strtolower();
/* Will trigger "Deprecated: strtolower(): Passing null to parameter #1 ($string)
of type string is deprecated"*/
strtolower(null);
Similarly passing a variable or typecasted int does not trigger an error, as shown in the two PHP examples below. In our examples, even though "1" isn't a string, we do not get an error and as a result 1 is returned from the function.
# No error is triggered, and 1 is returned
strtolower(1);
# No error is triggered, and 1 is returned
strtolower((int) 1);
The opposite function to lowercase a string in PHP is the strtoupper function. Working the same as strtolower, instead of lowering ASCII alphabetic characters, it uppercases them instead.
If you are looking to uppercase only the first letter, another similar string processing function to strtoupper is ucfirst. Where the PHP will uppercase the first letter of the string leaving the rest lowercase. Or to uppercase all the first letters of each word, you can use ucwords.
Conclusion
The strtolower function is a great way to convert a PHP string into lowercase. If you're looking to convert only ASCII characters, then this is the function for you.
- To convert a string to all lowercase use the strtolower function
- strtolower is a binary-safe function
- Only ASCII alphabetic characters are converted
- Use mb_strtolower to convert non-ASCII characters
- Since PHP 8.2, case conversion does not depend on the server's set locale
- Remember, don't pass a variable of type null, it must be a string