Using strtoupper() in PHP to Convert Strings to Uppercase

This article explains how to use PHP's strtoupper() function to convert strings to uppercase, covering its syntax, parameters, return values, and multiple code examples demonstrating handling of simple text, multi-word sentences, and strings containing numbers or special characters.

php Courses
php Courses
php Courses
Using strtoupper() in PHP to Convert Strings to Uppercase

In PHP, the strtoupper() function is commonly used to convert all alphabetic characters in a string to uppercase.

Basic Syntax of strtoupper()

strtoupper(string $string): string

Parameter Description

$string

: The string to be converted.

Return Value

The function returns the uppercase version of the input string.

Example 1

$str = "Hello World!";  // define a string variable
$result = strtoupper($str);  // convert to uppercase
echo $result;  // outputs: HELLO WORLD!

This example defines a string variable $str with the value "Hello World!", calls strtoupper() to convert it, stores the result in $result, and outputs the uppercase string.

Example 2

$str = "this is a sentence.";  // define a multi‑word string
$result = strtoupper($str);  // convert to uppercase
echo $result;  // outputs: THIS IS A SENTENCE.

The second example shows that the function works with strings containing multiple words, converting all lowercase letters to uppercase while leaving spaces unchanged.

Example 3

$str = "123ABC!@#";  // string with numbers and special characters
$result = strtoupper($str);  // convert to uppercase
echo $result;  // outputs: 123ABC!@#

This example demonstrates that strtoupper() only affects alphabetic characters; numbers and special symbols remain unchanged.

Overall, strtoupper() provides a simple way to transform lowercase letters to uppercase in PHP strings, while leaving non‑alphabetic characters untouched.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPTutorialString Manipulationuppercasestrtoupper
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.