Interesting ucfirst()

  • Avinash
  • 5
  • Aug 29, 2011
  • Web Development

For ucfirst() function we can say that one of the simple php function. But sometimes this simple function can also create a confusion for you.

Normally we perform this function on lower cased string. But have you tried to apply this function to Upper cased string.

Basically we can understand that ucfirst() will operate on whole string and make only first character to upper case. But this is not the case.

ucfirst() to lower cased string:

$str = "this is the string"; echo ucfirst($str); // OUTPUT // This is the string
$str = "this is the string";
echo ucfirst($str);
// OUTPUT
// This is the string

ucfirst() to upper cased string:

$str = "THIS IS THE STRING"; echo ucfirst($str); // OUTPUT // THIS IS THE STRING
$str = "THIS IS THE STRING";
echo ucfirst($str);
// OUTPUT
// THIS IS THE STRING

This is because, ucfirst() function will operate only on first character of the string rather than whole string.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • http://www.5jc.net huarong

    Can’t quite follow you.

    • http://www.xpertdeveloper.com Avinash

      Hi huarong,

      No problem at all, But actually I was surprised with the result. So i thought it is good to share :)

  • Karoly Bujtor

    Well, it seems logical that ucfirst() doesn’t change the case of any uppercase character in a string.

    You can use ucfirst(strtolower($str)) if you want only the first character uppercase and the others lowercase.

    • http://www.xpertdeveloper.com Avinash

      Yes that’s what I have done then after.. :)

  • Aaron

    Use ucwords() if you want to change “this is a test” into “This Is A Test.”