Fri, 17 May 2024


Strtolower and Strtoupper PHP Functions

By: Anton, NetArt Media
Wed, 1 August 2018
Strtolower and Strtoupper PHP Functions

Eventually, I will cover just about every built-in function within the PHP language, but I do like to choose my favorites and the ones I use most often while developing websites and applications. The strtolower function is definitely one of my favorites and while it doesn't offer a lot, given the right circumstances and contexts it can do exactly what we need.


Here we have an array of names that could be a list from a database or even just a saved txt file. If I wished to create links to each of their profile pages on the fly I could do it with the above mini-function. Which is how I might refer to it as.

I imploded the array, then exploded it accordingly. Using a foreach statement, I then created a specific configuration for each name in the array, using strtolower to lower the case of the first letter in each name and assigning 'npage' to represent the respective PHP files. This function, of course, creates a list of links to each person's profile page, with the anchor text of 'Visit Jeff's page', etc.

The example above should just about cover the usage of strtolower. It's quite self-explanatory. I can, however, also present an example of using the opposite function to it, 'strtoupper', as well as a similar function to it called 'ucfirst'.


So here the array of names is already lowered. We can take advantage of their natural state for the profile page names, but what about the actual names? Some people care about the case of a name, so we probably want to make them upper case and return them that way. All caps can be a bit much for some people to look at though, almost like they're being yelled at. ;-) In the event we only wanted to make the first letter of each name capitalized, we would use the PHP function 'ucfirst'.


As you may notice, all I've done in this code block was change 'strtoupper' to 'ucfirst'. As we might expect, it changes the first letter of each name to a capital. More often than not, data is saved to databases using lower case letters, so this function will come in just as handy as 'strtolower' and 'strtoupper' when appropriate.

I hope this PHP Tool Box article is helpful to you if you ever need a resource or a reminder. Play around with these functions and see what you can come up with. PHP is supposed to be fun. :-)


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox