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

青灯夜游
Release: 2023-03-16 17:54:02
Original
4527 people have browsed it

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)
Copy after login
ParametersDescription
stringRequired . 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);

?>
Copy after login

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)
Copy after login
  • 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);

?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template