ucfirst(): Converting the first char to upper case

$output=ucfirst($input_str);
ParameterDESCRIPTION
$input_strRequired : Input string
We can use php string function ucfirst() to change all first character of string to upper case

Now let us try with one example

Here is a string variable $str with one string inside it. We will use ucfirst() to capitalize first chars of the string.
$str="welcome to plus2net.com";
$str=ucfirst($str);
echo $str;
The output is here
Welcome To plus2net.com

DEMO

Output :

Example 2: Capitalizing the First Character After Trimming Whitespace

Use ucfirst() with trim() to capitalize the first character of a string that may contain leading whitespace.

$str = "   php tutorial";
$str = ucfirst(trim($str));
echo $str;  // Outputs: Php tutorial

Example 3: Handling Non-ASCII Characters

Since ucfirst() doesn’t handle multibyte characters, for non-ASCII strings, use *mb_ucfirst()*:

function mb_ucfirst($str) {
    return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
}
echo mb_ucfirst("élève"); // Outputs: Élève

String Functions ucwords(): To Change first char of all words to upper case of a string. how to change all the letters to upper case


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer