ucfirst

ucfirst EXPR
ucfirstThis function returns a version of
EXPR with the first character
titlecased and other characters left alone.
Titlecase is “Unicodese” for
an initial capital that has (or expects to have) lowercase characters
following it, not uppercase ones. Examples are the first letter of a
sentence, of a person’s name, of a newspaper headline, or of most words
in a title. Characters with no titlecase mapping return the uppercase
mapping instead. This is the internal function implementing the \u escape in double-quoted strings.
For example, if someone used U+FB02 latin
small ligature fl at the start of “flower” (that is, "\x{FB02}ower"), and you want to use it as the
first word of a sentence, its titlecase mapping is “Flower”, not “FLower”. Its uppercase is still “FLOWER”, though.
To capitalize a string by mapping its first character to titlecase and the rest to lowercase, use:
ucfirst(substr($word, 0, 1)) . lc(substr($word, 1))
Do not (unless you’re into cultural imperialism) use:
ucfirst lc $word
or "\u\L$word", because that
can produce a different and incorrect answer with certain characters.
The titlecase of something that’s been lowercased doesn’t always produce
the same thing titlecasing the original produces.
Because titlecasing only makes sense at the start of a string ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access