ucfirst

Converts the first character of a string to uppercase if it is an ASCII letter.

Signature
ucfirst(string $string): string
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$stringstringThe input string.

Return value

stringThe string with its first character uppercased, if that character is an ASCII letter a–z.

Changelog

  • PHP 8.2.0Case conversion no longer depends on the locale set with setlocale(). Only ASCII characters are converted.

ucfirst uppercases exactly one byte: the first one, and only if it falls in the range a to z. Everything else in the string is returned untouched, including the rest of the first word.

When to reach for it

It suits short machine-generated labels — turning an enum value such as pending into Pending for a status badge, or capitalising a single-word key in a debug dump. It is also common in code generators, where ucfirst($property) builds a getFoo() method name from a property name.

What trips people up

It does not lowercase anything. ucfirst('HELLO WORLD') is still HELLO WORLD; if you want sentence case you have to write ucfirst(strtolower($s)). Similarly, a leading space, digit or bracket means nothing happens at all — the function looks at position 0 and no further.

Since PHP 8.2 it is ASCII-only and locale-independent, so a name beginning with é or ß comes back unchanged. For any user-supplied text, use mb_convert_case($s, MB_CASE_TITLE, 'UTF-8'), and bear in mind that capitalising people's names automatically is usually wrong regardless of encoding — de Vries and van der Berg should stay as typed.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Turn a lowercase enum or status value into a display label such as 'Pending'.
  • Build a getter or setter method name from a property name in a code generator.
  • Capitalise the first word of a validation message assembled from lowercase fragments.

Frequently asked questions

Why does ucfirst not lowercase the rest of the string?

By design it only touches the first byte. Use ucfirst(strtolower($string)) when you want the rest of the string lowercased as well.

What is the difference between ucfirst and ucwords?

ucfirst capitalises only the first character of the whole string, while ucwords capitalises the first character of every word. Both leave the other characters untouched.

Does ucfirst work with accented characters?

No. Since PHP 8.2 it converts only ASCII a–z and ignores the locale, so a string starting with 'é' is returned unchanged. Use mb_convert_case with MB_CASE_TITLE for Unicode text.

More examples

Rendering status labels

Prints 'Pending', 'In review' and 'Done'. The strtolower is what makes 'DONE' come out right — ucfirst alone would have left it in caps.

Example · php
<?php
$statuses = ['pending', 'in_review', 'DONE'];

foreach ($statuses as $s) {
    echo ucfirst(str_replace('_', ' ', strtolower($s))), "\n";
}

Generating accessor names

Prints getName(), getEmailAddress() and getIsActive(). Because ucfirst leaves the remaining characters alone, the existing camelCase in 'emailAddress' survives exactly as written — which is the whole point here.

Example · php
<?php
$props = ['name', 'emailAddress', 'isActive'];

foreach ($props as $p) {
    echo 'public function get' . ucfirst($p) . '() { ... }', "\n";
}

Discussion

  • Be the first to comment on this lesson.