mb_ucfirst

Title-cases the first character of a string using Unicode rules, leaving the rest unchanged.

Signature
mb_ucfirst(string $string, ?string $encoding = null): string
  • Added in PHP 8.4.0

Parameters

ParameterTypeDescription
$stringstringThe input string.
$encodingoptional?string= nullCharacter encoding of the string. Falls back to the internal encoding when null.

Return value

stringThe string with its first character converted to title case; the remaining characters are returned unchanged.

Errors

Throws a ValueError if encoding names an unknown character encoding.

Added in PHP 8.4, mb_ucfirst() capitalises the first character of a string and leaves everything after it alone. It is the multibyte answer to ucfirst(), which operates on the first byte and therefore does nothing useful to élan — the first byte is only half of é, so ucfirst() returns the string unchanged.

When to reach for it

Use it for sentence-casing display text: a label, a validation message, a category name, the start of a paragraph. It is the right tool when you want to touch exactly one character and preserve the rest of the string verbatim, including any internal capitals such as iPhone or McDonald.

What trips people up

It applies title case, not uppercase. For almost every character these are the same, but not for digraphs: dzungla becomes Dzungla with the title-case form, while mb_strtoupper() on the first character would give DZungla. Title case is the linguistically correct choice here.

It also only touches the first character. If the input is ÉLAN, the result is still ÉLAN — lowercase the rest yourself first. And for capitalising every word you want mb_convert_case() with MB_CASE_TITLE, not a loop over mb_ucfirst(). An empty string comes back as an empty string.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Sentence-case a validation message built from a lowercase field name.
  • Capitalise a category or tag label that may start with an accented letter.
  • Fix user-entered names that were typed entirely in lowercase, without altering internal capitals.
  • Normalise the first letter of an imported product title from a CSV feed.

Frequently asked questions

Why does ucfirst not work with accented characters?

ucfirst() uppercases the first byte, and in UTF-8 an accented letter such as é is two bytes, so the first byte is not a letter on its own. mb_ucfirst() works on the first character instead and handles it correctly.

Which PHP version added mb_ucfirst?

PHP 8.4.0, together with mb_lcfirst(). Before that the usual workaround was mb_strtoupper(mb_substr($s, 0, 1)) . mb_substr($s, 1).

How do I capitalise every word in a UTF-8 string?

Use mb_convert_case($string, MB_CASE_TITLE) — it title-cases the first letter of each word. mb_ucfirst only ever changes the very first character of the whole string.

More examples

Sentence case versus title case

Every accented and non-Latin initial is capitalised correctly: Über, Ñandú, Ωμέγα, Istanbul. The last two lines show the difference in scope — mb_ucfirst gives "École normale" (one character changed) while MB_CASE_TITLE gives "École Normale" (every word changed).

Example · php
<?php
$words = ["über", "ñandú", "ωμέγα", "istanbul"];

foreach ($words as $w) {
    echo mb_ucfirst($w), " ";
}
echo "\n";

echo mb_ucfirst(mb_strtolower("ÉCOLE NORMALE")), "\n";
echo mb_convert_case("école normale", MB_CASE_TITLE), "\n";

Preserving the rest of the string

Only the first character changes, so "mcDonald" becomes "McDonald" with its internal capital intact and "iPhone-Hülle" becomes "IPhone-Hülle" — a reminder that blind capitalisation is wrong for brand names. mb_lcfirst does the reverse, turning "Ärztin" into "ärztin".

Example · php
<?php
$names = ["mcDonald", "iPhone-Hülle", "ärztin"];

foreach ($names as $n) {
    echo mb_ucfirst($n), "\n";
}
echo mb_lcfirst("Ärztin"), "\n";

Discussion

  • Be the first to comment on this lesson.