PHP Capitalization
At times, you may have a need to make sure certain words or characters are capitalized. For example, when using people’s names as strings, you would want to be sure the first letter of each name to be capitalized.
strtoupper
strtoupper will automatically capitalize every character of your string.
An example of using strtoupper can look like:
To put the above strtoupper example to use, we could code something like this:
echo “My string originally appeared as: $mystring <br />”;
echo “My new string now appears as: $mynewstring”;
This bit of code would display:
My string originally appeared as: I love my Apple Macbook
My new string now appears as: I LOVE MY APPLE MACBOOK
strtolower
strtolower, as the name suggests, is the opposite of strtoupper. Using strtolower will automatically convert every character of your string to lowercase.
An example of using strtolower can look like:
To put the above strtolower example to use, we could code something like this:
echo “My string originally appeared as: $mystring <br />”;
echo “My new string now appears as: $mynewstring”;
This bit of code would display:
My string originally appeared as: I love my Apple Macbook
My new string now appears as: i love my apple macbook
ucwords
As mentioned previously, at times you may want to be sure the first letter in a name is always capitalized. The ucwords function will automatically capitalize only the first character in each word.
An example of using ucwords can look like:
To put the above ucwords example to use, we could code something like this:
echo “My string originally appeared as: $mystring <br />”;
echo “My new string now appears as: $mynewstring”;
This bit of code would display:
My string originally appeared as: I love my Apple Macbook
My new string now appears as: I Love My Apple Macbook
One note to keep in mind when using capitalization functions is that it is different than the shift key on your keyboard. Only letters are affected. For example, when using a capitalization function, if the number 5 appears in your string, a “%” will not be outputted.




