String capitalization is used to change the case of a string. There are three functions for this purpose namely, strtoupper, strtolower and ucwords. Let’s have a look at each of these examples separately.
This function converts the entire specified string to upper case. Let’s see this with an example:
<?php $str="We capitalize this string!"; $str_upper=strtoupper($str); echo $str_upper; ?>
The output of the above PHP code will be:
WE CAPITALIZE THIS STRING!
By using this function, all the alphabets will be capitalized and the numbers and special characters will remain as they are.
This function converts the entire specified string to lower case. It’s functioning is similar to that of “strtoupper()” function.
Let’s see this example:
<?php $str="PHP is a good language!"; $str_lower=strtolower($str); echo $str_lower; ?>
The output of the above PHP script will be:
php is a good language!
This function capitalizes starting letter of each word in the string specified. This function comes in handy when writing a title or a heading. Let’s see this example that will demonstrate the function, ucwords():
<?php $str="This title will look good!"; $str_uc=ucwords($str); echo $str_uc; ?>
The output will be:
This Title Will Look Good!
This function ensures that the starting letters of each word in the string are capitalized. The rest of the string remains as it is. If there is a letter in upper case in the middle of the word, it will remain in upper case.