Overview of PHP ucfirst()
The ucfirst() is a pre-defined function in php used to convert each first character of a string 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.
This “alphabetic” is determined by the present locale. There are also ucwords() similar to ucfirst() where the strings used here are words that are basically a collection of many characters which is listed just after the delimiter parameter (which are space, newline, horizontal tab, vertical tab, etc.)
The ucfirst() function accepts only one parameter $string as its input which is also a mandatory field. This string will have its first character changed to uppercase which is represented by the parameter inside parentheses.
It returns the same string by only changing its first character to uppercase of the $string argument which is passed.
Examples of PHP ucfirst()
Given below are the examples of ucfirst():
A simple example to demonstrate the use of the ucfirst() function.
// 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
This a very simple example shown above. And as seen in the above example, we are passing a string as the input and assigning it to a variable $string. This $string is then passed as a parameter to the ucfirst function and wrapped in an echo function to display the output of the same. In the output, we can notice that the first letter of the statement is converted to upper case.
An example to show ucfirst() role when string already has the first letter in uppercase.
// 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
As shown in the above example, the sentence already has its first word in upper case. Hence when it is assigned to variable $string and displayed, it shows the same sentence without any changes.
An example to show usage of ucfirst() when 2 or more strings exist.
//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
$str2 = "this is second string";
//declaration of first string
In this example, we are showing how the ucfirst function is used to convert the first word of a sentence in 2 or more strings. First, we are assigning two different statements to two different parameters containing the string. Then using the ucfirst function we are converting 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.
$str1 = 'example for hello world!';
$str1 = ucfirst($str1);
$str2 = 'EXAMPLE FOR HELLO WORLD!';
$str2 = ucfirst($str2);
//use of strtolower() function
$str2 = ucfirst(strtolower($str2));
//use of lcfirst() function
$str2 = lcfirst($str2);
The above example signifies the use of ucfirst along with other functions like strtolower shown above. The function strtolower converts all the letters in the string to its lower case. Hence, we are combining strtolower with ucfirst to make the first letter as capital and then assigning the same to $str2 which is later displayed. The functionality of the lcfirst function, as shown above, is also the same as strtolower in the sense it also converts the string given to it to all lower-case letters.
This shows that other functions shown such as lcfirst, strtolower, and other such functions 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 different other functions.
PHP ucfirst() being a standalone function, it can perform only one thing that is to convert the first letter of the string passed to it to uppercase. Whether the input string first case be lower or upper case, ucfirst gives the output in upper case only. Like ucfirst() there are a lot of other functions in PHP like lcfirst(), strtolower(), strtoupper(), ucwords() which work in a very similar way and can be used for conversion of strings.
This is a guide to PHP ucfirst(). Here we discuss theoverview, syntax, and examples of PHP ucfirst(). You may also have a look at the following articles to learn more –