Skip to content
Tutorials Center
My learning

Topics

PHP Reference Β· String Functions

PHP ucfirst() function

Reference for PHP ucfirst(): make the first character of a string uppercase, what it does to the rest, multibyte limits and how to capitalize whole words.

ucfirst() returns a copy of a string with its first byte converted to uppercase if it is a lowercase ASCII letter. The rest of the string is left exactly as it was. It is handy for turning a lowercase word or a database value into the start of a sentence.

Because it works on bytes, only a to z change. An accented letter such as Γ© at the start stays unchanged, and an empty string comes back empty.

PHP
<?php
echo ucfirst("hello world");  // Hello world

Try it

PHP Capitalizing the first letter

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.

PHP Normalizing names and sentences

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

PHP
ucfirst(string $string): string
Name Description
$string The input string.

Return value: the string with its first character uppercased if it is an ASCII lowercase letter, otherwise the string unchanged.

Usage notes

  • Only the first byte is touched. ucfirst("hELLO") gives HELLO; combine with strtolower() to get Hello.
  • ASCII only. Since PHP 8.2 the function no longer depends on the current locale, so results are the same on every server. For Γ©, Γ±, ΠΆ and other letters use mb_strtoupper() with mb_substr().
  • A leading space blocks it. ucfirst(" hello") changes nothing because the first character is a space; trim() first.
  • For every word, use ucwords(). The opposite operation is lcfirst().
  • Use it as a callback by passing its name as a string, as in array_map('ucfirst', $words), or with the first-class callable syntax ucfirst(...).
  • Strings are immutable values. The original variable is not modified; assign the result.