Current Location: Home> Latest Articles> PHP Tips: Easily Convert the First Letter of a String to Lowercase

PHP Tips: Easily Convert the First Letter of a String to Lowercase

M66 2025-10-20

Converting the First Letter of a PHP String to Lowercase

Introduction

In PHP development, converting the first letter of a string to lowercase is a common requirement. This can be easily achieved using the built-in lcfirst() function or by combining strtolower() with substr(). This guide explains both methods in detail and provides examples and optimization tips.

Method One: Using the lcfirst() Function

The lcfirst() function is specifically designed to convert the first letter of a string to lowercase, while leaving the rest of the string unchanged. The syntax is:

string lcfirst(string $str)

Here, $str is the string to be processed.

Example:

$string = "Hello World";
$result = lcfirst($string); // Output: hello World

Method Two: Using strtolower() and substr() Functions

Another approach is to convert the entire string to lowercase using strtolower() and then manipulate the first character with substr(). The syntax is:

string strtolower(string $str)
string substr(string $str, int $start, int $length = null)

Here, $str is the string to be processed, $start is the starting position, and $length is the number of characters to replace.

Example:

$string = "Hello World";
$result = substr(strtolower($string), 0, 1) . substr($string, 1); // Output: hello World

Performance Comparison

In terms of efficiency, lcfirst() is faster than using strtolower() combined with substr() because it only processes the first character instead of the entire string.

Best Practices

  • For simple first-letter lowercase conversion, lcfirst() is recommended.
  • For more complex string manipulation, use strtolower() combined with substr().
  • Ensure that the variable contains a valid string.
  • Familiarize yourself with PHP string functions to avoid unexpected results.

Conclusion

In PHP, you can convert the first letter of a string to lowercase using either lcfirst() or the combination of strtolower() and substr(). lcfirst() is efficient and convenient, while strtolower() + substr() offers more flexibility. Choosing the appropriate method based on your specific needs helps improve code readability and performance.