Home > Article > Backend Development > PHP converts the first character in a string to uppercase using the function ucfirst()
Example
Convert the first character of "hello" to uppercase:
<?php echo ucfirst("hello world!"); ?>
Definition and usage
ucfirst() function converts the first character in the string to capital.
Related functions:
lcfirst() - Convert the first character in the string to lowercase
ucwords() - Convert the first character of each word in the string to uppercase
ucfirst(string)
Description | |
Required. Specifies the string to be converted. |
Returns the converted string. | |
4+ |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> ucfirst.php </title> <meta charset="UTF-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> <?php //原来首字符小写 $foo = 'hello world!'; $foo2 = ucfirst($foo); //输出 Hello world! echo $foo . "<br>"; echo $foo2 . "<br>"; //首字符大写,则不改变 $bar = 'HELLO WORLD!'; $bar = ucfirst($bar); //输出 HELLO WORLD! echo $bar . "<br>"; //将所有字符变成小写后,再将首字符编程大写 $bar = ucfirst(strtolower($bar)); //输出 Hello world! echo $bar . "<br>"; ?> </body> </html> hello world! Hello world! HELLO WORLD! Hello world!
The above is the detailed content of PHP converts the first character in a string to uppercase using the function ucfirst(). For more information, please follow other related articles on the PHP Chinese website!