PHP strtoupper() Function

Quick summary: The PHP strtoupper() function converts a string to uppercase.

PHP strtoupper() Syntax

strtoupper(string $string): string
PHP

PHP strtoupper() Basic examples

echo strtoupper("Hello world");
PHP
Output:
HELLO WORLD

Converts all characters to uppercase.

PHP strtoupper() Real-world usage

$code = "us";
echo strtoupper($code);
PHP
Output:
US

Normalizes country or state codes.

PHP strtoupper() Edge cases

echo strtoupper("ß");
PHP
Output:
ß

Certain characters are not converted correctly.

PHP strtoupper() Common mistakes

Using strtoupper with UTF-8 text

strtoupper does not handle multibyte characters.

Incorrect
echo strtoupper("ß");
Correct
echo mb_strtoupper("ß");

Use mb_strtoupper() for UTF-8 strings.

PHP strtoupper() Frequently Asked Questions

What does strtoupper() do in PHP?

strtoupper() converts all alphabetic characters in a string to uppercase.

Is strtoupper() multibyte-safe?

No, strtoupper() is not multibyte-safe. Use mb_strtoupper() for UTF-8 strings.

Does strtoupper() modify the original string?

No, strtoupper() returns a new string and does not modify the original variable unless reassigned.

What characters are affected by strtoupper()?

Only alphabetic ASCII characters are converted. Numbers and symbols remain unchanged.

What is the difference between strtoupper() and mb_strtoupper()?

strtoupper() handles ASCII only, while mb_strtoupper() supports multibyte encodings like UTF-8.

Does strtoupper() work with Unicode characters?

No, strtoupper() may not correctly convert Unicode characters. Use mb_strtoupper() instead.

Can strtoupper() be used for case-insensitive comparison?

Yes, converting both strings to uppercase allows case-insensitive comparison.

Is strtoupper() fast?

Yes, strtoupper() is a fast built-in function optimized for performance.

What happens if the string is already uppercase?

strtoupper() returns the string unchanged if it is already uppercase.

Can strtoupper() handle arrays?

No, strtoupper() works only on strings. Use array_map() to apply it to arrays.

What is a real-world use case of strtoupper()?

strtoupper() is commonly used for formatting, normalization, or preparing case-insensitive comparisons.

How to convert an entire string to uppercase safely?

Use mb_strtoupper($string, "UTF-8") for safe uppercase conversion in multibyte strings.

PHP strtoupper() Related PHP Functions