PHP Reference · String Functions
PHP strtoupper() function
Reference for PHP strtoupper(): convert a string to uppercase, why only ASCII letters change since PHP 8.2, and when to switch to mb_strtoupper().
strtoupper() returns a copy of a string with every lowercase ASCII letter (a to z) converted to uppercase. Digits, punctuation and non-ASCII bytes stay as they are.
Since PHP 8.2 the function ignores the locale and handles ASCII only. To uppercase accented letters in UTF-8 text, use mb_strtoupper(). The result for ß is a good example of why: the multibyte version turns it into SS.
<?php echo strtoupper("Hello world"); // HELLO WORLD
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
strtoupper(string $string): string
| Name | Description |
|---|---|
$string |
The input string. |
Return value: the uppercase string.
Usage notes
- ASCII only since PHP 8.2; before that it depended on
setlocale(LC_CTYPE). Do not rely on locale tricks. - Multibyte letters such as
é,жorßare left unchanged. Usemb_strtoupper(). - Same length. Because only single bytes change, the result has exactly the same byte length as the input; the multibyte version can change the length (
ßbecomesSS). - Case-insensitive comparison: use
strcasecmp()rather than converting both strings. - Only the first letter: see
ucfirst()anducwords(). - Do not use it for security decisions on user input such as file extensions without also validating the content.
Related
strtolower(),mb_strtoupper(),ucfirst(),ucwords()- Lessons: Strings, Multibyte strings