Convert String to Uppercase in PHP
In PHP, converting a string to uppercase is a straightforward task
thanks to built-in functions designed for handling both standard ASCII
and multibyte Unicode characters. This article explains how to use
strtoupper() for basic English strings and
mb_strtoupper() for multi-byte character sets like UTF-8,
complete with practical code examples.
Using strtoupper() for Standard Strings
The simplest way to convert a string to uppercase in PHP is by using
the strtoupper() function. This function takes a string as
its argument and returns the string with all alphabetic characters
converted to uppercase.
<?php
$lowercaseString = "hello world!";
$uppercaseString = strtoupper($lowercaseString);
echo $uppercaseString; // Outputs: HELLO WORLD!
?>Note: strtoupper() is locale-dependent and only
works reliably with standard ASCII characters. It will not correctly
convert accented characters or non-Latin alphabets.
Using mb_strtoupper() for Multibyte (UTF-8) Strings
If your application handles international characters, accents, or
non-Latin scripts (such as Spanish, Cyrillic, or German), you should use
mb_strtoupper(). This multibyte-safe function ensures that
characters like “é” or “ñ” are capitalized correctly.
<?php
$multibyteString = "café";
// Standard strtoupper() fails on "é"
echo strtoupper($multibyteString); // Outputs: CAFé (or broken characters)
// mb_strtoupper() handles it correctly
echo mb_strtoupper($multibyteString, 'UTF-8'); // Outputs: CAFÉ
?>Key Differences Summary
| Function | Best Used For | Multibyte/UTF-8 Safe |
|---|---|---|
strtoupper() |
English-only ASCII strings | No |
mb_strtoupper() |
Multi-language and UTF-8 strings | Yes |