mb_strtoupper

Converts all alphabetic characters in a string to uppercase using Unicode case rules.

Signature
mb_strtoupper(string $string, ?string $encoding = null): string
  • Added in PHP 4.3.0

Parameters

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

Return value

stringThe string with every character that has the Unicode 'alphabetic' property converted to uppercase.

Errors

Throws a ValueError if encoding names an unknown character encoding.

mb_strtoupper() uppercases every character Unicode marks as alphabetic, so é becomes É and ω becomes Ω. It applies full Unicode uppercase mappings, which means the result can be longer than the input: German ß uppercases to two characters, SS. Plain strtoupper() is ASCII-only since PHP 8.2 and leaves all of this alone.

When to reach for it

Use it for display text you want shouted — headings, table labels, initials — and for normalising codes that are conventionally uppercase, such as country codes, currency codes and licence plates. It is also the safe way to fold text before storing it in a case-insensitive index when the data is not guaranteed to be ASCII.

What trips people up

Uppercasing is not reversible. Once ß has become SS, lowercasing gives you ss, and character counts shift, which breaks any code that assumed mb_strlen() stays constant. Digraphs behave differently again: dz uppercases to DZ but title-cases to Dz.

Like the rest of mbstring, the conversion ignores locale, so it cannot produce the Turkish dotted İ from i. And uppercasing does not strip accents — É is not E, which surprises people building search keys.

Example

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

When to use it

  • Render a heading or table header in all caps without mangling accented characters.
  • Normalise a two-letter region code such as 'de' into 'DE' for a locale identifier.
  • Uppercase a reference number or licence plate entered in mixed case.
  • Build a display-safe shout line from Greek or Cyrillic source text.

Frequently asked questions

Why does mb_strtoupper turn ß into SS?

Unicode defines the uppercase mapping of ß as the two-character sequence SS, and mb_strtoupper applies the full Unicode mapping. This makes the result one character longer than the input, so do not assume uppercasing preserves string length.

What is the difference between mb_strtoupper and mb_convert_case?

mb_strtoupper is equivalent to mb_convert_case with MB_CASE_UPPER. mb_convert_case additionally offers MB_CASE_TITLE for per-word capitalisation and MB_CASE_FOLD for comparison keys.

Does mb_strtoupper remove accents?

No. É stays É — uppercasing changes case, not diacritics. To strip accents use iconv with //TRANSLIT or the intl Transliterator.

More examples

Normalising a locale code

mb_strtoupper turns the region subtag into DE so the code reads de_DE. The last two lines contrast the two ways to capitalise: MB_CASE_TITLE capitalises each word and gives "Jean-Luc Picard", while mb_strtoupper shouts the whole thing.

Example · php
<?php
$code = "de-de";

[$lang, $region] = explode("-", $code);
echo $lang . "_" . mb_strtoupper($region), "\n";

echo mb_convert_case("jean-luc picard", MB_CASE_TITLE), "\n";
echo mb_strtoupper("jean-luc picard"), "\n";

Uppercase versus ASCII strtoupper

mb_strtoupper converts every letter including Å, ü and ω. strtoupper only touches ASCII, so the accented and Greek characters come through unchanged and the two results differ — var_dump prints false.

Example · php
<?php
$name = "Ångström über Ωμέγα";

echo mb_strtoupper($name), "\n";
echo strtoupper($name), "\n";
var_dump(mb_strtoupper($name) === strtoupper($name));

Discussion

  • Be the first to comment on this lesson.