lcfirst

Lowercases the first character of a string, leaving the rest of the string untouched.

Signature
lcfirst(string $string): string
  • Added in PHP 5.3.0

Parameters

ParameterTypeDescription
$stringstringThe input string.

Return value

stringThe input string with its first character lowercased, 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.

lcfirst lowercases exactly one character — the first byte of the string — and copies everything after it verbatim. It is the mirror image of ucfirst, and it is just as narrow: lcfirst('HELLO WORLD') returns hELLO WORLD, not hello world. If you want the whole string lowercased, that is strtolower.

When to reach for it

Almost always when converting between naming conventions. Turning StudlyCase class names or Total Price labels into camelCase keys is a two-step move: build the StudlyCase form with ucwords, then knock the first letter down with lcfirst. It also shows up when you are stitching a sentence together and the first word came from a title-cased source.

ASCII only

Since PHP 8.2 the conversion is fixed to ASCII: only the bytes A (0x41) through Z (0x5A) are changed, and setlocale no longer influences the result. A string starting with É, Ä or any multi-byte character comes back unchanged, because lcfirst looks at the first byte, not the first Unicode character. For accented input use mb_lcfirst, which mbstring gained in PHP 8.4; on older versions fall back to mb_strtolower(mb_substr($s, 0, 1)) . mb_substr($s, 1).

An empty string is returned as an empty string; no error, no warning.

Example

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

When to use it

  • Convert a StudlyCase or Title Case label into a camelCase array key or JSON property name.
  • Turn snake_case database columns into camelCase properties when hydrating an object.
  • Lower the first letter of a title-cased phrase before splicing it into the middle of a sentence.

Frequently asked questions

What is the difference between lcfirst and strtolower?

lcfirst lowercases only the first character and leaves the rest of the string exactly as it was; strtolower lowercases every character. Use lcfirst for camelCase conversion and strtolower when you want the whole string folded.

Does lcfirst work with accented or UTF-8 characters?

No. It operates on the first byte and only converts ASCII A–Z, so strings beginning with characters such as É or Ä are returned unchanged. Use mb_strtolower on the first multi-byte character instead.

Is there an mb_lcfirst function in PHP?

Yes. mb_lcfirst was added in PHP 8.4, alongside mb_ucfirst, and lowercases the first Unicode character correctly: mb_lcfirst($s). On PHP 8.3 and older there is no such function, and the usual replacement is mb_strtolower(mb_substr($s, 0, 1)) . mb_substr($s, 1).

More examples

Only the first character changes

Output is `Apple -> apple`, `BANANA -> bANANA`, `cherry -> cherry`. `BANANA` proves the point: only the leading `B` is lowered, the rest of the word keeps its case. And `cherry` is returned untouched because its first character is already lowercase.

Example · php
<?php
$words = ['Apple', 'BANANA', 'cherry'];
foreach ($words as $w) {
    printf("%-8s -> %s\n", $w, lcfirst($w));
}

snake_case to camelCase

Prints `createdAt`, `shippingAddressLine1` and `id`. `ucwords($snake, '_')` capitalises each underscore-delimited word, `str_replace` removes the underscores, and `lcfirst` fixes the one character that ended up wrongly capitalised. The `id` case shows the function is a safe no-op for single-word input.

Example · php
<?php
function camel(string $snake): string {
    return lcfirst(str_replace('_', '', ucwords($snake, '_')));
}

echo camel('created_at'), PHP_EOL;
echo camel('shipping_address_line_1'), PHP_EOL;
echo camel('id'), PHP_EOL;

Discussion

  • Be the first to comment on this lesson.