Home  >  Article  >  Backend Development  >  What is the function in php to convert the first letter from lower case to upper case?

What is the function in php to convert the first letter from lower case to upper case?

青灯夜游
青灯夜游Original
2022-02-10 16:25:063507browse

Function to convert the first letter from lowercase to uppercase in php: 1. ucfirst() function, which can convert the first letter of a string to uppercase, the syntax is "ucfirst($str)"; 2. ucwords() function, You can convert the first letter of each word in a string to uppercase with the syntax "ucwords($str)".

What is the function in php to convert the first letter from lower case to upper case?

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

The functions to convert the first letter from lower case to upper case in php are Two types:

  • ucfirst() function, which can convert the first letter of the string into uppercase

  • ucwords() function, which can convert the characters Convert the first letter of each word in the string to uppercase

1. ucfirst() function

ucfirst() function converts the first letter of each word in the string to uppercase The first character is converted to uppercase.

Example:

<?php
echo ucfirst("hello world!");
?>

What is the function in php to convert the first letter from lower case to upper case?

2. ucwords() function

ucwords() function The first character of each word in the string is converted to uppercase.

Grammar:

ucwords($string [, $delimiters = "\t\r\n\f\v" ])

Among them, $string is the string that needs to be converted; $delimiters is an optional parameter, used to represent word delimiters. The default is space and tab. character, line feed character, carriage return character, horizontal line and vertical bar.

Example:

<?php
$foo = &#39;hello world!&#39;;
$foo = ucwords($foo);             // Hello World!
echo $foo."<br>";

$foo = &#39;hello|world!&#39;;
$bar = ucwords($foo);             // Hello|world!
echo $bar."<br>";

$baz = ucwords($foo,"|");        // Hello|World!
echo $baz."<br>";
?>

Output:

Hello World!
Hello|world!
Hello|World!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the function in php to convert the first letter from lower case to upper case?. 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