This guide will show you how to convert a string to uppercase in PHP using strtoupper()
function. This is extremely helpful when you want to manipulate the string quickly to all uppercase characters. You might use this for names, addresses, or even product names.
In order to make this work we first need to establish a string that we want to make uppercase. Here I create a variable $myLowerCaseString
and have a sentence that is in all lowercase letters.
// This is our all lower case string, that we want converted to upper case
$myLowerCaseString = "this is all lower case, but i want it upper case";
Next we use the strtoupper()
function that will uppercase the letters in the string automatically for us. In the below line of code I simply echo
the string that is generated.
// Using strtoupper() we convert all of our strings characters to upper case.
echo strtoupper($myLowerCaseString);
Below is the output from the code we wrote above. You can see that all of the characters are uppercase and it did not impact the comma that was in the string.
THIS IS ALL LOWER CASE, BUT I WANT IT UPPER CASE
As you can see it was pretty easy to convert the string to uppercase in PHP using the strtoupper()
function.
The full code might look something like below when we put it all together. There are many different use cases for making an entire string uppercase. If you are having troubles or want to share what projects you work on that requires making entire strings uppercase, please drop a comment below!
// This is our all lower case string, that we want converted to upper case
$myLowerCaseString = "this is all lower case, but i want it upper case";
// Using strtoupper() we convert all of our strings characters to upper case.
echo strtoupper($myLowerCaseString);