php

Lowercase first character of a String in PHP

If you need to lowercase the first character of a string in PHP, you can use the lcfirst() function. This function takes a string as an argument and returns the string with the first character converted to lowercase.

<?php
  echo lcfirst("Foo bar");

  // -> foo bar
?>

Output

foo bar

The lcfirst() function specifically is used to convert the first character of a string to lowercase. This is the most sufficient way to convert the first character of the string to lowercase.

In the above code example, we have passed a string "Foo bar" to the lcfirst() function and it is returning "foo bar" as output.

Syntax

lcfirst($str)

More examples of lcfirst() function

<?php
  echo lcfirst("We are programmers");
  // -> we are programmers
  
  echo lcfirst("HELLO WORLD");
  // -> hELLO WORLD
?>

Convert first character to lowercase using strtolower() function

As we know that the strtolower() function is used to convert all the characters of a string to lowercase. Here we will get the first character using string index - $str[0] and then convert it to lowercase. We then replace this character with the first character of the string.

Code example

<?php
  $str = "Hello WORLD";
  $str[0] = strtolower($str[0]);
  
  echo $str;
?>

Output

hello WORLD

The PHP code sets the string variable $str to "Hello WORLD".

The code converts the first letter of the string to lowercase using the strtolower() function.

The code outputs the string.

Was this helpful?