Search the blog

If you want to make the first character of a string uppercase using PHP you might be tempted to use ucwords(). The problem is ucwords() also makes all the other characters lowercase.

For example, john Smith becomes John smith.

Here’s how to make the first character uppercase while leaving the rest of the characters as they are.

$string    = 'my name is John Smith.';
$firstChar = mb_strtoupper(mb_substr($string, 0, 1)); // Get the first character
$string    = mb_substr($string, 1);                   // Get everything except the first character
$string    = $firstChar . $string;                    // 'My name is John Smith.'

Note: I am using mb_* functions to support UTF-8, etc.

Tim Bennett is a freelance web designer from Leeds. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.