You are reading the article A Comprehensive Overview To Php Ucfirst() updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 A Comprehensive Overview To Php Ucfirst()Overview of 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
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.Examples of PHP ucfirst()
Given below are the examples of ucfirst():Example #1
A simple example to demonstrate the use of the ucfirst() function.
Code:<?php $string = "example for ucfirst"; echo(ucfirst($string));
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.Example #2
An example to show the ucfirst() role when the string already has the first letter in uppercase.<?php $string = "Just an example"; echo(ucfirst($string));
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.Example #3
An example to show usage of ucfirst() when 2 or more strings exist.
Code:<?php $str1 = "this is first string"; echo ucfirst($str1); echo "n"; $str2 = "this is second string"; echo ucfirst($str2);
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"); $str2 = ucfirst(strtolower($str2)); echo($str2); echo("n"); $str2 = lcfirst($str2); echo($str2);
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.Conclusion
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.
This is a guide to PHP ucfirst(). Here we discuss the overview, syntax, and examples of PHP ucfirst() with output and code implementation. You may also have a look at the following articles to learn more –
You're reading A Comprehensive Overview To Php Ucfirst()
Update the detailed information about A Comprehensive Overview To Php Ucfirst() on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!