Home >Backend Development >PHP Tutorial >How to convert the first letter of a string to uppercase or lowercase in php?
I told you how to convert the case of a string to uppercase or lowercase. However, sometimes we don’t need to completely convert the letters of the string to uppercase or lowercase, but only need to uppercase the first letter of the string. conversion or lowercase conversion.
So, this article will lead you to understand how to convert the first letter of a string to uppercase or lowercase.
Converting the first letter of a string to uppercase or lowercase can be divided into the following situations:
The first one: Every Convert the first letter of each word to uppercase
To convert the first letter of each word in the string to uppercase, we use: ucwords() function, the syntax is as follows:
ucwords(string)
Parameter string is the string to be converted
Example
<?php echo ucwords("hello world"); ?>
Code running result:
Second case: Convert the first letter of the first word to uppercase
To convert the first letter of the first word to uppercase, we use: ucfirst() function, the syntax is as follows:
ucfirst(string)
Parameter string For the string to be converted
Example
<?php echo ucfirst("php hello!"); ?>
The result of running the code:
ucfirst() function will only convert the The first letter of a word is converted to uppercase, and subsequent words will not be converted.
The third case: the first letter of the first word is converted to lowercase
Convert the first letter of the first word to lowercase, we use: lcfirst() function, the syntax is as follows:
lcfirst(string)
The parameter string is the string to be converted
Example
<?php echo lcfirst("Php Hello!"); ?>
Code running results:
lcfirst() function converts the letters of the first word to lowercase, instead of converting the first letter of each word To lowercase
Convert the first letter of the string to uppercase or lowercase. Let’s follow what we said earlierHow does php convert the string to uppercase or lowercase? The usage is similar, if you are interested, you can check it out.
【Related recommendations】
1. Special topic recommendation: php string
2. Video tutorial: "php.cnDugujiujian (4)-php video tutorial》
3. php practical video tutorial
The above is the detailed content of How to convert the first letter of a string to uppercase or lowercase in php?. For more information, please follow other related articles on the PHP Chinese website!