Home > Article > Backend Development > How to convert the first letter of a string to uppercase in php
Conversion method: 1. Use the ucfirst() function to convert the first letter of the entire string to uppercase letters. The syntax "ucfirst(string)" will return the converted string. 2. Use the ucwords() function to convert the first letter of each word in the string to uppercase letters. The syntax "ucwords(string)" will return the converted string.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Convert the first letter of the string to uppercase Two situations:
Convert the first letter of the entire string to uppercase
Convert the first letter of each word in the string to uppercase
Corresponding to different situations, different PHP functions need to be used.
1. Use the ucfirst() function to convert the first letter of the entire string to uppercase.
The ucfirst() function converts the first character of the string to uppercase.
ucfirst(string)
Parameters | Description |
---|---|
string | Required . Specifies the string to be converted. |
Return value: Returns the converted string.
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello world!'; echo "原字符串:".$str; echo "<br><br>首字母大写:".ucfirst($str); ?>
2. Use the ucwords() function to convert the first letter of each word in the string to uppercase
ucwords() function converts the first character of each word in the string to uppercase.
ucwords(string)
Return value: Returns the converted string.
<?php header("Content-type:text/html;charset=utf-8"); $str = 'hello world!'; echo "原字符串:".$str; echo "<br><br>首字母大写:".ucwords($str); ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to convert the first letter of a string to uppercase in php. For more information, please follow other related articles on the PHP Chinese website!