Skip to content

Core\Str::lowerFirst

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

Changes the first letter of a text to lower case.

Core\Str::lowerFirst takes one string and returns a copy where only the first character is in lower 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 lower case, or is not a letter, the text does not change. An empty string gives "".

Core\Str::upperFirst does the opposite. Core\Str::lower 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 lower case. An empty string gives "".

Examples

Only the first letter

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

Letters with accents

<?nvs
// Shows that `Core\Str::lowerFirst` 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::lowerFirst($word), "\n";
}
Output
ärger
élan
ωμέγα

Keys for json

<?nvs
// Shows how to turn column names like `OrderTotal` into JSON keys like `orderTotal`.
array<string> $columns = ['OrderId', 'OrderTotal', 'ShippingAddress'];
// Only the first letter changes, so the other capitals stay where they are.
// This prints `orderId,orderTotal,shippingAddress`.
array<string> $keys = [];
foreach ($columns as string $column) {
$keys[] = Core\Str::lowerFirst($column);
}
echo Core\Str::join($keys, ','), "\n";
Output
orderId,orderTotal,shippingAddress