Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to capitalize the first letter of a string

Short answer

For ASCII text use ucfirst():

<?php
 
echo ucfirst('hello');

Output:

Hello

Non-ASCII text

ucfirst() changes only ASCII letters. PHP 8.4 added mb_ucfirst() for any alphabet:

<?php
 
echo mb_ucfirst('привет');

Output:

Привет

PHP before 8.4

In older versions, uppercase the first letter separately and append the rest of the string:

<?php
 
$text = 'привет';
 
echo mb_strtoupper(mb_substr($text, 0, 1)) . mb_substr($text, 1);

Output:

Привет

Functions used in this recipe

  • ucfirst — Makes the first letter of a string uppercase.
  • mb_strtoupper — Converts a string to uppercase.
  • mb_substr — Returns part of a string.

Thanks for reading!

Donate to the site — we work hard for you.