strtoupper
Converts all ASCII lowercase letters in a string to uppercase.
strtoupper(string $string): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | The input string. |
Return value
stringThe input string with every ASCII character in the range a–z converted to its uppercase equivalent.
Changelog
- PHP 8.2.0Case conversion no longer depends on the locale set with setlocale(). Only ASCII characters a–z are converted.
strtoupper subtracts 32 from every byte between a (0x61) and z (0x7A) and leaves every other byte alone. It is binary-safe, allocation-cheap and completely encoding-unaware.
When to reach for it
Use it for machine-facing values that are ASCII by definition: HTTP verbs, currency and country codes, ticket prefixes, hexadecimal digests, enum names. It is also the right tool when you are generating a string that a protocol requires in uppercase.
What trips people up
Since PHP 8.2 the function is locale-independent and touches only a–z, so é stays é and ß stays ß. Code that used setlocale to uppercase accented text silently stopped working on 8.2 — the call still succeeds, it just does less than before.
The other classic mistake is using uppercasing for display. Turning a sentence into caps in PHP bakes the change into your data; a CSS text-transform rule keeps the original text intact for copy-paste, screen readers and search. And uppercasing is a poor way to compare strings — strcasecmp does it in one step, and for Unicode you want mb_convert_case with MB_CASE_FOLD rather than a naive uppercase on both sides.
Example
When to use it
- Normalise a three-letter currency or country code before matching it against a table.
- Uppercase an HTTP method name so 'get' and 'GET' route the same way.
- Build a human-readable reference prefix such as 'INV-' + strtoupper($hash).
Frequently asked questions
Why does strtoupper leave accented letters unchanged?
As of PHP 8.2 it converts only the ASCII range a–z and no longer consults the locale. Use mb_strtoupper($string, 'UTF-8') to uppercase accented or non-Latin characters.
Should I use strtoupper or CSS text-transform to display text in capitals?
Use CSS for presentation. strtoupper changes the stored value, which breaks copy-paste, search and screen readers; text-transform: uppercase keeps the real text intact.
Is strtoupper faster than mb_strtoupper?
Yes, substantially — it is a plain byte loop with no encoding tables. Only reach for mb_strtoupper when the string can contain non-ASCII characters that must be converted.
More examples
Building a reference code
Prints something like 'INV-00042-9C4E1A'. md5() returns lowercase hex, so strtoupper is what gives the reference its conventional all-caps look; substr trims it to a readable length.
<?php
$id = 42;
$suffix = substr(strtoupper(md5('order-' . $id)), 0, 6);
echo 'INV-' . str_pad((string) $id, 5, '0', STR_PAD_LEFT) . '-' . $suffix, "\n";Normalising an HTTP verb before dispatch
Prints 'list', 'create' and 'method not allowed'. Uppercasing the incoming verb lets a single canonical route table handle any casing the caller sends.
<?php
$routes = ['GET' => 'list', 'POST' => 'create'];
foreach (['get', 'Post', 'delete'] as $verb) {
$key = strtoupper($verb);
echo $verb, ' -> ', $routes[$key] ?? 'method not allowed', "\n";
}
Discussion