Using ucfirst to make the first letter of a string uppercase

Using ucfirst to make the first letter of a string uppercase

When building web applications with PHP, string manipulation will make up a large part of the code you write, and formatting strings so they appear consistent, for example making every page heading start with an uppercase is just one of those. Or you might be displaying people's names or usernames on pages such as the profile section, and data stored in your database is always consistent. So to combat that, you can use one of PHP's many built-in functions to help format strings in a consistent manner. To do this you can use PHP's ucfirst function that allows developers to do just that. Let's jump in and explore how it works, along with some examples of why it might not work.

PHP ucfirst

The ucfirst function makes up a collection of string functions in PHP. With ucfirst as the name suggested, uppercase the first letter in a given string. The function takes one string parameter and returns a string back, as you can see below.

ucfirst(string $string): string

Let's take a few examples to see it in action. In our first example, the ucfirst function has transformed 'hello world' into 'Hello world' with the 'H' being changed to uppercase. You'll notice that it hasn't changed 'world' to have an uppercase 'W'. That's because this function only changes the first letter in the string.

# Outputs: Hello world
ucfirst('hello world');

With that being said, it only changes characters that are in the ASCII character range of 0x61 - 0x7a (a - z). Let's see this in action where we have numbers at the start of the string to see what happens. Because the numbers '1 2 3' are outside of the ASCII range that the function transforms, those numbers are left unchanged and, like the example above, will change the string has an uppercase 'H'.

# Outputs: 1 2 3 Hello world
ucfirst('1 2 3 hello world');

One common mistake developers make with this function is what happens to pre-existing uppercase letters. Let's take another example, where the first letter of the string is already in uppercase. Here the string 'Abc' already has an uppercase 'A' and therefore the ucfirst will not make any changes, which is what generally we'd expect and want. The function wouldn't, in this case, change the string to 'ABc', making the 'B' uppercase, as this would be against the purpose of the function, "making the first letter uppercase in a string".

# Outputs: Abc
echo ucfirst('Abc');

Another misconception is when a string is already all in uppercase letters. This function wouldn't revert the string to all lowercase in this instant, and in fact, the string would remain unchanged, because the first letter is already in uppercase, as the example below shows. If you wanted just the first letter to be uppercase and the others lowercase, you would in fact have to convert the string to all lowercase first (using strtolower) before passing it through ucfirst.

# Outputs: ABCDEFG
echo ucfirst('ABCDEFG');

As of PHP 8.2, this function no longer relies on the setting of setlocale as only ASCII characters are converted. It is also now deprecated to pass 'null' to this function as this will trigger the following error "Deprecated: ucfirst(): Passing null to parameter #1 ($string) of type string is deprecated".

# Triggers a deprecated warning
$string = null;
echo ucfirst($string);

Passing anything else other than a string will also trigger a fatal type error. In the example below, we're passing an array to the function but the function only expects a string. Therefore a fatal "TypeError" is triggered; similar to "Fatal error: Uncaught TypeError: ucfirst(): Argument #1 ($string) must be of type string, array given".

# Triggers a fatal error
$string = [];
echo ucfirst($string);

To avoid this, don't pass anything that isn't a string by checking it first before using it. A simple and quick way to do that is using the is_string method in PHP. This function is great because you're allowed to pass any mixed data type, and PHP will tell you if the type of the variable supplied is a string. This added protection can make your code more robust, especially if you cannot guarantee the data type of variable that is passed to it.

$string = [];

if (is_string($string)) {
 echo ucfirst($string);
}

Conclusion

Using the ucfirst function in PHP is a great way to ensure consistent strings throughout your web application, and ensure they always have an uppercase first letter.

  • The ucfirst will only uppercase the first letter of the string
  • The ucfirst function will only uppercase ASCII characters within the a - z range
  • If the string already has an uppercase letter at the start, nothing will be transformed
  • Don't pass any other types other than a string to this function, otherwise, you'll trigger a fatal PHP error

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all