For [code]ucfirst()[/code] 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 [code]ucfirst()[/code] will operate on whole string and make only first character to upper case. But this is not the case.
ucfirst() to lower cased string:
[cc lang=”php”]
$str = “this is the string”;
echo ucfirst($str);
// OUTPUT
// This is the string
[/cc]
ucfirst() to upper cased string:
[cc lang=”php”]
$str = “THIS IS THE STRING”;
echo ucfirst($str);
// OUTPUT
// THIS IS THE STRING
[/cc]
This is because, [code]ucfirst()[/code] function will operate only on first character of the string rather than whole string.
Can’t quite follow you.
Hi huarong,
No problem at all, But actually I was surprised with the result. So i thought it is good to share :)
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.
Yes that’s what I have done then after.. :)
Use ucwords() if you want to change “this is a test” into “This Is A Test.”