PHP strtoupper Function: Convert Strings to Uppercase

PHP Uppercase Strings

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 = "[email protected]";
echo strtoupper($text);

The output:

[email protected]

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 = "[email protected]";
$cleanEmail = strtolower($email);

echo $cleanEmail;

Output:

[email protected]

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?

It converts all letters in a string to uppercase.

Does strtoupper() change numbers?

No. It leaves numbers and symbols as they are.

How do I convert non-English letters to uppercase?

Use mb_strtoupper() with UTF-8 encoding.

Can I use strtoupper() on an array?

Not directly. Use array_map("strtoupper", $array).

Is strtoupper() case-insensitive?

No. It only converts the string. You must use it with a comparison for case-insensitive checks.

What happens if I pass an empty string?

It returns an empty string.

Similar Reads

PHP abs Function: How to Get Absolute Values

The abs() function in PHP gives you a number without its sign — it always returns a positive value. That…

PHP array_multisort Function with Examples

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…

Check If a Row Exists in PDO

At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…

Inheritance in PHP: Share Code from Class to Class & Examples

PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…

PHP Variable Basics: Everything You Need to Know

Think about the PHP variable as little storage boxes holding your data, whether it is a number, a word, or…

PHP Array: Accessing and Managing Elements

PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

PHP strpos Function: How it Works with Examples

Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…

PHP explode Function: How it Works with Examples

When you deal with text in PHP, sometimes you need to break it into parts. This is common when users…

PHP $GLOBALS: Access Global Variables with Examples

When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…

Understanding the PHP Operator Precedence

The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values…

Previous Article

PHP strpos Function: How it Works with Examples

Next Article

JavaScript Loops and Iteration: How They Work with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.