ucfirst(): Converting the first char to upper case

Uppercase the first character

ucfirst() uppercases the first lowercase ASCII character when it is the first character of the string. It does not trim whitespace or provide Unicode title-casing. The multibyte example below requires the mbstring extension.

$text = 'php tutorial';
echo ucfirst($text); // Php tutorial
$output=ucfirst($input_str);
ParameterDESCRIPTION
$input_strRequired : Input string
Use ucfirst() to change only the first character of a string to uppercase when it is an ASCII lowercase letter.

For example:

The remaining characters are unchanged, even when later words begin with lowercase letters.
$str="welcome to plus2net.com";
$str=ucfirst($str);
echo $str;
The output is here
Welcome to plus2net.com

Try ucfirst() with your own text

Enter a string to see the converted result. The submitted value is escaped before it is returned to this HTML page.

Result:


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, define a helper using mb_substr() and mb_strtoupper() (requires mbstring):

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