Skip to content

Core\Str::upperFirst

Core\Str::upperFirst(string $s): stringCore\Str::upperFirst(
    string $s,
): string

Changes the first letter of a text to upper case.

Core\Str::upperFirst takes one string and returns a copy where only the first character is in upper case. The rest of the text stays exactly as it is, so "ärger" gives "Ärger". It uses the rules of Unicode, so letters with accents and letters from other alphabets change too.

If the first character is already in upper case, or is not a letter, the text does not change. An empty string gives "". A few letters become two letters in upper case, so "ß" at the start becomes "SS".

Core\Str::lowerFirst does the opposite. Core\Str::upper changes every letter, not only the first one.

Parameters

  • $sstringThe string whose first character changes.

Return value

Returns string. The string with its first character in upper case. An empty string gives "".

Examples

Only the first letter

<?nvs
// Shows that `Core\Str::upperFirst` changes the first letter and keeps the rest.
// This prints `Hello world`. The `w` stays in lower case.
echo Core\Str::upperFirst('hello world'), "\n";
// A first character that is not a letter does not change. This prints `42 apples`.
echo Core\Str::upperFirst('42 apples'), "\n";
// An empty text gives an empty text. This prints an empty line.
echo Core\Str::upperFirst(''), "\n";
Output
Hello world
42 apples

Letters with accents

<?nvs
// Shows that `Core\Str::upperFirst` also changes a first letter with an accent or from another alphabet.
array<string> $words = ['ärger', 'élan', 'ωμέγα'];
// This prints `Ärger`, `Élan` and `Ωμέγα`.
foreach ($words as string $word) {
echo Core\Str::upperFirst($word), "\n";
}
Output
Ärger
Élan
Ωμέγα

Status labels

<?nvs
// Shows how to turn stored status values like `pending` into labels for a page, like `Pending`.
array<string> $statuses = ['pending', 'shipped', 'on hold'];
// Only the first letter changes, so `on hold` becomes `On hold`.
// This prints `Pending`, `Shipped` and `On hold`, one on each line.
foreach ($statuses as string $status) {
echo Core\Str::upperFirst($status), "\n";
}
Output
Pending
Shipped
On hold