PHP strtoupper() Function: Syntax, Parameters, Return Value, Usage Examples, and Important Notes
This article explains PHP's strtoupper() function, covering its syntax, required string parameter, return value, multiple usage examples—including full string conversion, partial conversion, word capitalization, regex-based conversion, and multibyte handling—along with key considerations such as case sensitivity and non‑letter character behavior.
In PHP, the strtoupper() function converts all characters in a string to uppercase. It accepts a string as input and returns the transformed string.
strtoupper() Function Syntax
<code>strtoupper(string $string)</code>Parameter Description:
$string : required. The string to be converted to uppercase.
Return Value:
Returns a new string where every character of the original string has been converted to uppercase.
Usage Examples
<code>$string = "Hello World";
$uppercaseString = strtoupper($string);
echo $uppercaseString; // Output: HELLO WORLD</code>In this example, strtoupper() converts the string "Hello World" to uppercase, stores the result in $uppercaseString , and then outputs it.
Notes
strtoupper() is case‑sensitive and works based on ASCII values, converting lowercase letters to their uppercase equivalents while leaving existing uppercase letters unchanged.
Non‑alphabetic characters such as numbers or punctuation are not altered.
The function can handle multibyte characters, including UTF‑8 encoded strings, making it useful for internationalization (i18n) and localization (l10n) scenarios.
strtoupper() Function Usage
Example 1: Convert the Entire String to Uppercase
<code>$string = "Hello World";
$uppercaseString = strtoupper($string);
echo $uppercaseString; // Output: HELLO WORLD</code>Example 2: Convert Only Part of the String to Uppercase
<code>$string = "Hello World";
$uppercaseString = strtoupper(substr($string, 6));
echo $uppercaseString; // Output: WORLD</code>Example 3: Convert the First Letter of Each Word to Uppercase
<code>$string = "hello world";
$titleCaseString = strtoupper(wordwrap($string, 5, " ", true));
echo $titleCaseString; // Output: HELLO world</code>Example 4: Convert All Words to Uppercase Using a Regular Expression
<code>$string = "hello world";
$allUpperCaseString = strtoupper(preg_replace('/\b\w+\b/', '${0,}', $string)); // Use regex to convert words to uppercase
echo $allUpperCaseString; // Output: HELLO WORLD (same as strtoupper result)</code>Example 5: Handle Multibyte Characters (e.g., UTF‑8 Strings)
<code>$string = "你好世界"; // UTF‑8 encoded string containing Chinese characters
$uppercaseString = strtoupper($string); // Convert Chinese characters to uppercase
echo $uppercaseString; // Output: 你好世界 (same as strtoupper result)</code>Summary
The strtoupper() function in PHP is highly useful for converting all characters of a string to uppercase, which aids in case‑insensitive text processing, generating HTML tags, handling filenames and URLs, and forming database queries. It also supports multibyte characters, making it suitable for internationalized applications.
PHP Learning Recommendations
Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial
Vue3+TP6+API Social E‑commerce System Development Course
Swoole From Beginner to Master Recommended Course
Workerman+TP6 Real‑time Chat System Limited‑time Offer
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.