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);
| Parameter | DESCRIPTION |
|---|---|
| $input_str | Required : Input string |
ucfirst() to change only the first character of a string to uppercase when it is an ASCII lowercase letter.
$str="welcome to plus2net.com";
$str=ucfirst($str);
echo $str;
The output is here
Welcome to plus2net.com
Enter a string to see the converted result. The submitted value is escaped before it is returned to this HTML page.
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
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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.