W3docs

ucfirst()

The ucfirst() function in PHP is used to capitalize the first character of a string. In this article, we will discuss the ucfirst() function in detail and how

ucfirst() returns a copy of a string with its first character converted to uppercase. Every other character is left exactly as it was. It is the function you reach for when you need to capitalize a name, a sentence, or a label without touching the rest of the text.

This page covers the syntax, the return value, the edge cases that surprise people (numbers, already-uppercase strings, multibyte text), and how ucfirst() differs from the related capitalization functions.

Syntax

ucfirst(string $string): string
ParameterDescription
$stringThe input string.

Return value: a new string with the first character uppercased. ucfirst() does not modify $string in place — it returns the result, so you must capture or echo it. If the first character is not a lowercase letter (a digit, a symbol, or already uppercase), the string is returned unchanged.

Basic example

php— editable, runs on the server

Only the leading h becomes H. The comma, the rest of the words, and the exclamation mark are untouched — ucfirst() never lowercases or changes anything after the first character.

What ucfirst() does not do

A common mistake is expecting ucfirst() to clean up the whole string. It does not:

<?php

echo ucfirst("HELLO") . "\n";   // HELLO  (first char already uppercase → no change)
echo ucfirst("hELLO") . "\n";   // HELLO  (only the first char is touched, not the rest)
echo ucfirst("123abc") . "\n";  // 123abc (first char is a digit → nothing to capitalize)

If you want a clean Capitalized word from messy input, lowercase it first, then capitalize:

<?php

$messy = "hELLO";
echo ucfirst(strtolower($messy));   // Hello

Capitalizing every word

ucfirst() only affects the very first character of the whole string. To capitalize the first letter of every word, use ucwords() instead:

<?php

$title = "the quick brown fox";

echo ucfirst($title) . "\n";   // The quick brown fox
echo ucwords($title) . "\n";   // The Quick Brown Fox

Multibyte / non-ASCII text

ucfirst() works byte by byte and only understands ASCII letters (az). It will not correctly uppercase accented or non-Latin first letters such as é, ñ, or Cyrillic characters. For those, lowercase-aware handling needs the multibyte string extension — there is no mb_ucfirst(), so a common pattern is:

<?php

function mb_ucfirst(string $string, string $encoding = "UTF-8"): string
{
    $first = mb_strtoupper(mb_substr($string, 0, 1, $encoding), $encoding);
    return $first . mb_substr($string, 1, null, $encoding);
}

echo mb_ucfirst("élise");   // Élise

When to use it

  • Formatting user input — display "john" typed in a form as "John".
  • Sentence casing — capitalize the first word of a generated message or label.
  • Building readable identifiers — turn a slug segment into a heading.

For the opposite operation (lowercasing the first character) see lcfirst(). To change the case of the entire string, see strtoupper() and strtolower().

Summary

ucfirst() uppercases only the first character of a string and returns the result without modifying the original. It ignores non-letters and already-uppercase characters, never touches the rest of the string, and is ASCII-only — combine it with strtolower() to normalize messy input, with ucwords() to capitalize every word, or with the multibyte functions above for accented text.

Practice

Practice
What does the PHP ucfirst() function do?
What does the PHP ucfirst() function do?
Was this page helpful?