Home >Backend Development >PHP Tutorial >PHP converts the first character in a string to uppercase using the function ucfirst()

PHP converts the first character in a string to uppercase using the function ucfirst()

黄舟
黄舟Original
2017-11-06 13:54:531653browse

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

  • ##strtoupper() - Convert the string to uppercase

  • strtolower( ) - Convert the string to lowercase

Syntax

ucfirst(string)

##ParametersstringTechnical details
Description
Required. Specifies the string to be converted.

Return value: PHP version: This function demonstrates how to add the first string to the string using the ucfirst function. Characters converted to uppercase
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 = &#39;hello world!&#39;;
$foo2 = ucfirst($foo); //输出 Hello world!
echo $foo . "<br>";
echo $foo2 . "<br>";
//首字符大写,则不改变
$bar = &#39;HELLO WORLD!&#39;;
$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn