How to capitalize the first letter of a string
Short answer
For ASCII text use ucfirst():
<?php
echo ucfirst('hello');Output:
HelloNon-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.