How to convert the first letter of a string to uppercase in PHP

You can utilize the PHP ucfirst() capacity to change the main character of a string to uppercase (for example capital). Then again, you can utilize the strtolower() work in mix with the ucfirst() work, in the event that you need to make just first letter of the string to uppercase and rest of the string to lowercase. 



(PHP 4, PHP 5, PHP 7)

ucfirst - Capitalize first character

ucfirst (string $ str): string
Returns the string str after replacing the first character with its capital letter, if the first character is alphabetic.

Note that the notion of "alphabetic" is determined by the location configuration. This means that for the default configuration "C", characters such as accented vowels (like é, è or à) will not be converted.

(PHP 4, PHP 5, PHP 7)

strtolower - Returns a string in lowercase

strtolower (string $ string): string
Returns string, after converting all alphabetic characters to lowercase.

We should look this exemple:


  • <?php
  • $str1 = 'the color of the sky is blue.';
  • echo ucfirst($str1);
  • echo "<br>";
  • result:The color of the sky is blue.
  • $str2 = 'the Color of the Sky is Blue.';
  • echo ucfirst(strtolower($str2));
  • result:The color of the sky is blue.

  • ?>
if you want to improve the solution
comment to help the community

No comments
Post a Comment

Post a Comment