PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I make the first letter of a string uppercase?


If this question has been keeping you awake, then you will sleep soundly after reading this.

The answer to your enquiry is the function called ucfirst() which doesn't mean you were the first person to see, but rather UpperCaseFirst.

Like this:
<?php
$unexpected 
" how are you today?";
echo 
ucfirst($unexpected);
// how are you today?
?>


Note that you didn't get the expected result here, and that's because the first character is actually a space.

The lesson is, when using this function it is best to always pass the string through trim first to get rid of any leading space, so you always get the expected results:

<?php
$expected 
" how are you today?";
echo 
ucfirst(trim($expected));
//How are you today?
?>



More strings PHP Questions

How do I make the contents of a string upper case?
How can I repeat a string variable several times?
How do I escape a string with slashes?
How can I remove spaces from around a string value?
How do I join strings together?