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.
In PHP, the strtoupper() function is commonly used to convert all alphabetic characters in a string to uppercase.
Basic Syntax of strtoupper()
<code>strtoupper(string $string): string</code>Parameter Description
$string : The string to be converted.
Return Value
The function returns the uppercase version of the input string.
Example 1
<code>$str = "Hello World!"; // define a string variable
$result = strtoupper($str); // convert to uppercase
echo $result; // outputs: HELLO WORLD!</code>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
<code>$str = "this is a sentence."; // define a multi‑word string
$result = strtoupper($str); // convert to uppercase
echo $result; // outputs: THIS IS A SENTENCE.</code>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
<code>$str = "123ABC!@#"; // string with numbers and special characters
$result = strtoupper($str); // convert to uppercase
echo $result; // outputs: 123ABC!@#</code>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.
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.