Home > Article > Backend Development > PHP ucfirst()
The ucfirst() is a pre-defined function in php used to convert each string’s first character from lowercase to uppercase. It takes a string as its input and returns the same with the first character of the string capitalized in case the character is alphabetic.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
This “alphabetic” is determined by the present locale. There are also ucwords() similar to ucfirst() where the strings used here are words that are a collection of many characters which is listed just after the delimiter parameter (which are space, newline, horizontal tab, vertical tab, etc.)
Syntax:
ucfirst(string<em> $str)</em>
The ucfirst() function accepts only one parameter $string as its input which is also a mandatory field. This string modifies its first character to uppercase, represented by the parameter inside parentheses.
It returns the exact string, modifying only its first character to uppercase from the passed $string argument.
Given below are the examples of ucfirst():
A simple example to demonstrate the use of the ucfirst() function.
Code:
<?php // This is an example of PHP code which // shows the functioning of ucfirst $string = "example for ucfirst"; // converts the string assigned to $string to uppercase // and displays the output echo(ucfirst($string)); ?>
Output:
This is a straightforward example shown above. And as seen in the above example, we pass a string as the input and assign it to a variable $string. We pass this $string as a parameter to the ucfirst function, then wrap it in an echo function to display the output. We notice in the output that the statement’s first letter becomes uppercase.
An example to show the ucfirst() role when the string already has the first letter in uppercase.
Code:
<?php // This is an example of a PHP program which // shows how the ucfirst works when // the string having first case already in upper case $string = "Just an example"; // Since the first case is already upper case // it displays exact same in output echo(ucfirst($string)); ?>
Output:
As shown in the above example, the sentence already has its first word in the upper case. Therefore, the sentence appears unchanged when we assign it to the variable $string and display it.
An example to show usage of ucfirst() when 2 or more strings exist.
Code:
<?php //This is an example of PHP code which //shows how to use ucfirst function for 2 //or more strings $str1 = "this is first string"; //declaration of first string echo ucfirst($str1); echo "\n"; $str2 = "this is second string"; //declaration of first string echo ucfirst($str2); ?>
Output:
In this example, we show how the ucfirst function converts the first word of a sentence into 2 or more strings. First, we assign two different statements to two different parameters containing the string. Then using the ucfirst function, we convert the first letter of both sentences to uppercase. Like this, we can convert how many ever strings we want using the ucfirst function.
An example to show ucfirst() in conjunction with other similar PHP functions.
Code:
<?php $str1 = 'example for hello world!'; $str1 = ucfirst($str1); echo($str1); echo("\n"); $str2 = 'EXAMPLE FOR HELLO WORLD!'; $str2 = ucfirst($str2); echo($str2); echo("\n"); //use of strtolower() function $str2 = ucfirst(strtolower($str2)); echo($str2); echo("\n"); //use of lcfirst() function $str2 = lcfirst($str2); echo($str2); ?>
Output:
The above example signifies the use of ucfirst and other functions like strtolower. The function strtolower converts all the letters in the string to their lowercase. Therefore, we combine strtolower with ucfirst to capitalize the first letter and then assign the result to $str2 for later display. The lcfirst function, as demonstrated above, functions similarly to strtolower because it converts the provided string to all lowercase letters.
This shows that other functions, such as lcfirst, strtolower, and others, can be used in conjunction with ucfirst functions to get the desired string output. It also shows the usage of two strings, $str1 and $str2, and how to pass the same string parameter to other functions.
PHP ucfirst(), being a standalone function, can perform only one thing: to convert the first letter of the string to uppercase. Whether the input string first case is lower or upper case, ucfirst only gives the output in upper case. Like ucfirst(), there are a lot of other functions in PHP like lcfirst(), strtolower(), strtoupper(), and ucwords() which work in a very similar way and can be used for the conversion of strings.
The above is the detailed content of PHP ucfirst(). For more information, please follow other related articles on the PHP Chinese website!