Table of Content
Use strtoupper() function when you want to change all letters in a string to uppercase in PHP. It works with standard ASCII characters.
Understand the Uppercase Function (strtoupper) in PHP
The strtoupper() changes every letter in a string to its uppercase version. It does not touch numbers or symbols. It works on ASCII strings, so it may not handle accented or non-English characters as expected.
strtoupper($string)You pass in a text. It gives you a new string in all uppercase.
Here is a quick example:
$name = "john doe";
$upper = strtoupper($name);
echo $upper;The output:
JOHN DOE
This example takes a name with lowercase letters and turns it into full uppercase. The function does not change spaces or punctuation.
But, how strtoupper() handles numbers and symbols?
It leaves numbers and spaces alone. It only changes letters.
For example:
$text = "user123@example.com";
echo strtoupper($text);The output:
USER123@EXAMPLE.COM
The numbers and the @ symbol stay the same.
You can use strtoupper() on both sides when you compare strings. That does not care about the case.
$userInput = "yes";
$answer = "YES";
if (strtoupper($userInput) === strtoupper($answer)) {
echo "Match";
}This makes the check work no matter how the user types their answer.
Difference Between strtoupper() and mb_strtoupper()
strtoupper() only works well with basic English characters. It may not convert everything if your text uses other languages or accents.
mb_strtoupper() handles multibyte characters like (é, ü, ç). You must use the correct encoding (usually UTF-8).
$text = "français";
echo strtoupper($text);
echo mb_strtoupper($text); Output:
FRANÇAIS (wrong result)
FRANÇAIS (correct)
Examples of Uppercase Function in PHP
Convert an array of strings to uppercase:
$colors = ["red", "green", "blue"];
$upperColors = array_map("strtoupper", $colors);
print_r($upperColors);Output:
["RED", "GREEN", "BLUE"]
This runs strtoupper() on every item in the array.
Uppercase multibyte strings with mb_strtoupper():
$text = "schön";
$upper = mb_strtoupper($text, "UTF-8");
echo $upper; Output:
SCHÖN
ö would stay lowercase without mb_strtoupper(). This function handles it the right way.
Normalize user input for email:
$email = "user@Example.com";
$cleanEmail = strtolower($email);
echo $cleanEmail;Output:
user@example.com
You can combine strtolower() and strtoupper() based on your goal. Use strtolower() for emails, and strtoupper() for codes or labels.
Format product codes:
$code = "abc-123";
$code = strtoupper($code);
echo $code;Output:
ABC-123
Product codes often need consistent casing. strtoupper() helps keep them uniform.
Wrapping Up
You learned how to use strtoupper() to convert strings to uppercase. You also saw how it behaves with symbols and arrays with multibyte text. Also, you now know when to use strtoupper() and when to switch to mb_strtoupper().
Here is a quick recap:
- Use
strtoupper()for basic ASCII text - Use
mb_strtoupper()for non-English characters - Combine with arrays and other functions for more control
- Helpful in case-insensitive checks and formatting
Thank you for reading. Click here to see more PHP tutorials.
FAQs
What does strtoupper() do in PHP?
Does strtoupper() change numbers?
How do I convert non-English letters to uppercase?
Can I use strtoupper() on an array?
Is strtoupper() case-insensitive?
What happens if I pass an empty string?
Similar Reads
PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…
let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…
Abstract class in PHP appeared to provide a way to define a structure for related classes and don't allow direct…
PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…
The PHP ternary operator gives you a way to write short expressions. It reduces lines of code and avoids long…
PHP and MySQL have become an inseparable pair for web developers. One handles the logic, while the other stores the…
The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…
The PHP break statement helps in controlling the flow of loop work. It gives you a way to stop a…
This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…
The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…