Search code snippets, questions, articles...

Uppercase first character of a String in PHP

If you need to uppercase the first character of a string in PHP, you can use the ucfirst() function. This function takes a string as an argument and returns a new string with the first character uppercased.
  echo ucfirst("we are programmers");
  // -> We are programmers
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

To convert the first character of the string to uppercase:

  1. Get the string that you want to convert the first letter to upercase.
  2. Pass the string to ucfirst() function and it will return the string with first letter uppercase.



# Method 2: Using strtoupper() function

If you do not want to use the ucfirst() function then you can also use strtoupper() function. It converts all the characters of the string to uppercase.

We will get the first character of the string and convert it to uppercase. Then replace it with the first character of the string. Check the below example:

  $str = "hello world";
  $str[0] = strtoupper($str[0]);
  echo $str;


Hello world

In the above code example, we have a string called $str.

We are getting the first letter from the string using $str[0] and then passing it to strtoupper() function that will return the uppercase format of this letter.

Then replace it with the first letter of the string and print it.

Was this helpful?