Today I come with some useful PHP functions which are very useful to understand case functions of PHP.
Case functions change the string from lowercase to uppercase and vice versa and those functions are strtolower,strtoupper, ucfirst and ucwords. strtolower function decapitalizes entire strings whereas strtoupper function capitalizes entire strings. Other case functions operate only on first letters of words which are.ucwords and ucfirst.
Lets checkout functions one by one:
1. strtolower():
The strtolower() function returns an all lowercase string. It doesn’t matter if the original is all uppercase or mixed,just returned string will be in lowercase.
Description
string strtolower (string str)
It returns the string with all alphabetic characters converted to lowercase.
Note that ‘alphabetic’ is determined by the current locale settings. This means that when “C” locale default characters such as umlaut-A will not be converted.
Lets see example:
1 2 3 4 5 | $original = "PHP is recursive acronym of Hypertext Preprocessor"; $lower = strtolower($original); echo $lower; |
OUTPUT
PHP is recursive acronym of hypertext preprocessor
strtolower() is extremely easy to use.you can decapitalize e-mail addresses contain capital letters.
2. strtoupper()
The strtoupper() function returns an all uppercase string, in spite of whether the original was all lowercase or mixed,It will capitalize the string.
1 2 3 4 5 | $original ="PHP is recursive acronym of Hypertext Preprocessor"; $upper = strtoupper($original); echo $upper; |
OUTPUT:
PHP IS RECURSIVE ACRONYM OF HYPERTEXT PREPROCESSOR
3. ucfirst()
The ucfirst() function which capitalizes only the first character of a string.
1 2 3 4 5 | $original ="php is recursive acronym of Hypertext Preprocessor"; $ucfirst=ucfirst($original); echo $ucfirst; |
OUTPUT
Php is recursive acronym of Hypertext Preprocessor
4. ucwords()
The ucwords() function capitalizes the first character of each word in a string:
1 2 3 4 5 | $original ="php is recursive acronym of Hypertext Preprocessor"; $ucwords=ucwords($original); echo $ucwords; |
OUTPUT
Php Is Recursive Acronym Of Hypertext Preprocessor
Neither ucwords() nor ucfirst() converts anything into lowercase. Each makes only the
appropriate significant characters into uppercase. If there are inappropriate capital characters in the middle of words, they will not be converted.
Do let me know case functions of PHP is useful to you. I would love to hear your opinions & experiences in the comments section below.
If you enjoyed reading this article, do share it your friends who are working in PHP.