How to Use strtoupper in PHP: Convert Strings to Uppercase
Use the
strtoupper function in PHP to convert all letters in a string to uppercase. Pass the string as an argument like strtoupper('hello'), which returns 'HELLO'.Syntax
The strtoupper function takes one string argument and returns a new string with all alphabetic characters converted to uppercase.
- string: The input string you want to convert.
- Returns: A string with all letters in uppercase.
php
string strtoupper(string $string)
Example
This example shows how to convert a lowercase string to uppercase using strtoupper.
php
<?php
$input = 'hello world';
$output = strtoupper($input);
echo $output;
?>Output
HELLO WORLD
Common Pitfalls
One common mistake is forgetting that strtoupper does not change the original string but returns a new one. Also, it only affects alphabetic characters and leaves numbers and symbols unchanged.
Another pitfall is passing non-string types without converting them first, which can cause warnings.
php
<?php // Wrong: expecting original string to change $word = 'hello'; strtoupper($word); echo $word; // outputs 'hello', not uppercase // Right: assign the result $word = 'hello'; $word = strtoupper($word); echo $word; // outputs 'HELLO' ?>
Output
hello
HELLO
Quick Reference
| Function | Description | Example | Output |
|---|---|---|---|
| strtoupper | Converts string to uppercase | strtoupper('php') | 'PHP' |
| strtolower | Converts string to lowercase | strtolower('PHP') | 'php' |
| ucfirst | Capitalizes first letter | ucfirst('php') | 'Php' |
Key Takeaways
Use strtoupper to convert all letters in a string to uppercase in PHP.
strtoupper returns a new string; it does not modify the original variable.
Only alphabetic characters are changed; numbers and symbols stay the same.
Always assign the result of strtoupper to a variable if you want to keep the uppercase string.