Home  >  Article  >  Backend Development  >  How to convert the first letter of a string to uppercase in php

How to convert the first letter of a string to uppercase in php

青灯夜游
青灯夜游Original
2022-08-17 19:50:144535browse

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.

How to convert the first letter of a string to uppercase in php

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 = &#39;hello world!&#39;;
echo "原字符串:".$str;
echo "<br><br>首字母大写:".ucfirst($str);

?>

How to convert the first letter of a string to uppercase in php

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 = &#39;hello world!&#39;;
echo "原字符串:".$str;
echo "<br><br>首字母大写:".ucwords($str);

?>

How to convert the first letter of a string to uppercase in php

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!

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