PHP - String Capitalization Functions
PHP - String Capitalization Functions
If you've ever wanted to manipulate the capitalization of your PHP strings, then this lesson will be quite
helpful to you. PHP has three primary capitalization related functions: strtoupper, strtolower and ucwords. The
function names are pretty self-explanatory, but why they are useful in programming might be new to you.
The strtoupper function takes one argument, the string you want converted to upper case and returns the
converted string. Only letters of the alphabet are changed, numbers will remain the same.
PHP Code:
$originalString = "String Capitalization 1234";
$upperCase = strtoupper($originalString);
echo "Old string - $originalString <br />";
echo "New String - $upperCase";
Display:
Old string - String Capitalization 1234
New String - STRING CAPITALIZATION 1234
One might use this function to increase emphasis of a important point or in a title. Another time it might be
used with a font that looks very nice with all caps to fit the style of the web page design.
A more technical reason would be to convert two strings you are comparing to see if they are equal. By
converting them to the same capitalization you remove the possibility that they won't match simply because of
different capitalizations.
The strtolower function also has one argument: the string that will be converted to lower case.
PHP Code:
$originalString = "String Capitalization 1234";
$lowerCase = strtolower($originalString);
echo "Old string - $originalString <br />";
echo "New String - $lowerCase";
Display:
Old string - String Capitalization 1234
New String - string capitalization 1234