Fabio Fabio - 2 years ago 125
PHP Question

ucfirst not working properly?

I'm probably missing something really obvious.

While converting a bunch of string before inserting them in a array I noticed some string where different among each other because of first char being uppercase or not. I decided then to use

to make first character uppercase but it seems it doesn't work properly, I have had a look around on the web trying to figure out why this is happening but I had no luck.

$produtto = 'APPLE';
echo ucfirst($produtto);
//output: APPLE

If I use instead

$produtto = 'APPLE';
echo mb_convert_case($produtto, MB_CASE_TITLE, "UTF-8");
//Output: Apple

Answer Source

ucfirst() only looks at the first character so you should convert to lowercase first.

Use this:

$produtto = 'APPLE';
echo ucfirst(strtolower($produtto));
//output: Apple
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download