Home > Backend Development > PHP Tutorial > How to convert the first letter of a string to uppercase or lowercase in php?

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

怪我咯
Release: 2023-03-07 18:14:01
Original
3304 people have browsed it

I told you how to convert the case of a string to uppercase or lowercase. However, sometimes we don’t need to completely convert the letters of the string to uppercase or lowercase, but only need to uppercase the first letter of the string. conversion or lowercase conversion.

So, this article will lead you to understand how to convert the first letter of a string to uppercase or lowercase.

Converting the first letter of a string to uppercase or lowercase can be divided into the following situations:

The first one: Every Convert the first letter of each word to uppercase

To convert the first letter of each word in the string to uppercase, we use: ucwords() function, the syntax is as follows:

ucwords(string)
Copy after login

Parameter string is the string to be converted

Example

<?php
echo ucwords("hello world");
?>
Copy after login

Code running result:

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

Second case: Convert the first letter of the first word to uppercase

To convert the first letter of the first word to uppercase, we use: ucfirst() function, the syntax is as follows:

ucfirst(string)
Copy after login

Parameter string For the string to be converted

Example

<?php
echo ucfirst("php hello!");
?>
Copy after login

The result of running the code:

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

How to convert the first letter of a string to uppercase or lowercase in php?ucfirst() function will only convert the The first letter of a word is converted to uppercase, and subsequent words will not be converted.

The third case: the first letter of the first word is converted to lowercase

Convert the first letter of the first word to lowercase, we use: lcfirst() function, the syntax is as follows:

lcfirst(string)
Copy after login

The parameter string is the string to be converted

Example

<?php
echo lcfirst("Php  Hello!");
?>
Copy after login

Code running results:

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

How to convert the first letter of a string to uppercase or lowercase in php?lcfirst() function converts the letters of the first word to lowercase, instead of converting the first letter of each word To lowercase

Convert the first letter of the string to uppercase or lowercase. Let’s follow what we said earlierHow does php convert the string to uppercase or lowercase? The usage is similar, if you are interested, you can check it out.

【Related recommendations】

1. Special topic recommendation: php string

2. Video tutorial: "php.cnDugujiujian (4)-php video tutorial

3. php practical video tutorial

The above is the detailed content of How to convert the first letter of a string to uppercase or lowercase in php?. For more information, please follow other related articles on the PHP Chinese website!

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