PHP strtoupper

Summary: in this tutorial, you’ll learn how to use the PHP strtoupper() function to convert all alphabetic characters of a string to uppercase.

Introduction to the PHP strtoupper() function #

The strtoupper() function accepts a string and returns a new string with all alphabetic characters converted to uppercase.

The following shows the syntax of the strtoupper() function:

strtoupper ( string $string ) : stringCode language: PHP (php)

The strtoupper() uses the current locale to determine alphabetic characters.

To return a new string in a specific encoding with all alphabetic characters converted to uppercase, you use the mb_strtoupper() function instead:

mb_strtoupper ( string $string , string|null $encoding = null ) : stringCode language: PHP (php)

The following defines a upper() function that returns a new string with all characters converted uppercase:

<?php

if (!function_exists('upper')) {
    function upper(string $value) : string
    {
        return mb_strtoupper($value, 'UTF-8');
    }
}Code language: PHP (php)

To return a new string with all alphabetic characters converted to lowercase, you use the strtolower() function.

PHP strtoupper() function examples #

The following example uses the strtoupper() function to convert the string php to uppercase:

<?php

echo strtoupper('php');Code language: PHP (php)

Output:

PHPCode language: PHP (php)

Summary #

  • Use the strtoupper() function to return a new string from a string with all characters converted to uppercase.
  • Use the mb_strtoupper() to convert a string with a specific encoding to uppercase.
Did you find this tutorial useful?