[Solved]: PHP ucwords() function not working
  John Mwaniki /   01 Nov 2021

[Solved]: PHP ucwords() function not working

You may be using ucwords() PHP function to convert words in your PHP strings but it doesn't seem to work.

This is a common scenario where I have found many PHP developers complaining about it not working. I as well faced the same when I started using it in the early days.

ucwords() is a built-in function in PHP for converting the first character of every word in a PHP string to uppercase.

Example

<?php
$string = "hello world! my name is john. i like programming in php language.";
echo ucwords($string);
?>

Output:

Hello World! My Name Is John. I Like Programming In Php Language.

As you can see from the output, each word now starts with an uppercase letter.

Why ucwords() function may not work and how to fix it

From the explanation above, ucwords() converts the first character of every word in the string to uppercase.

This means that it only converts the case of the first letter/character of the word but does not convert the case of the other characters in the word.

If the string has all or some of the words in full uppercase, then no change will happen to those words despite using ucwords() function.

Example

<?php
$string = "HELLO WORLD! MY NAME IS JOHN. I LIKE PROGRAMMING IN PHP LANGUAGE.";
echo ucwords($string);
?>

Output:

HELLO WORLD! MY NAME IS JOHN. I LIKE PROGRAMMING IN PHP LANGUAGE.

As you can see from the output, nothing has been changed on the string. Since each word already starts with an uppercase letter, nothing had to be changed.

The fix to ucwords() not working

To fix the ucwords() function and ensure that each word in a PHP string starts with an uppercase letter and the rest of the characters in the word are in lowercase, you first need to convert the string into lowercase using strtolower() function, then pass the lowercase string into ucwords() function.

Example

<?php
$string = "HELLO WORLD! MY NAME IS JOHN. I LIKE PROGRAMMING IN PHP LANGUAGE.";
echo ucwords(strtolower($string));
?>

Output:

Hello World! My Name Is John. I Like Programming In Php Language.

From the example above, you can see we have first converted the string to lowercase then used the ucwords() function to make every word start with uppercase.

Though the above code works well and solves the problem, I do not advise you to use it as it is directly. This should be used only when you already know in advance that all words are in uppercase. Else, you should first check the case before converting the string to lowercase.

This is because as much as you want to change the case, you may want to retain the original case of some words in the string. For example, you may want abbreviations(eg. HTML, UN, UNESCO, WHO, etc) to retain their original uppercase. If you subject the whole string with them to lowercase conversion then use ucwords(), they will end up like Html, Un, Unesco, Who, etc which may change their original meanings.

How to check if all characters in a PHP string are in uppercase

It is very easy to check if all the characters in a PHP string are in uppercase. All you need to do is to convert the string to uppercase using the strtoupper() function and compare the output with the original string. If they happen to be equal, then definitely the string is in uppercase.

Example

<?php
$string = "HELLO WORLD! MY NAME IS JOHN. I LIKE PROGRAMMING IN PHP LANGUAGE.";
if(strtoupper($string) == $string){
 echo "The string is uppercase";
}
else{
 echo "The string is not uppercase";
}
?>

Output:

The string is uppercase

To handle non-ascii use:

<?php
mb_strtoupper($string, 'utf-8') == $string
?>

Before using ucwords() function, check whether the string is all uppercase. If so, first convert to lowercase the use ucwords(). Else, use ucwords() directly and this will perfectly solve your problem.

Example

<?php
$string = "HELLO WORLD! MY NAME IS JOHN. I LIKE PROGRAMMING IN PHP LANGUAGE.";
if(strtoupper($string) == $string){
 echo ucwords(strtolower($string));
}
else{
 echo ucwords($string);
}
?>

The PHP ucfirst() function not working

Note: The above solution will also solve the case of ucfirst() function. This function is used to convert PHP strings into sentence cases, where the first letter of the first word in a sentence is converted to uppercase. The solution will work exactly the same. All you need is to replace ucwords() with ucfirst().

Example

<?php
$string = "HELLO WORLD! MY NAME IS JOHN. I LIKE PROGRAMMING IN PHP LANGUAGE.";
if(strtoupper($string) == $string){
 echo ucfirst(strtolower($string));
}
else{
 echo ucfirst($string);
}
?>

Output:

Hello World! My Name Is John. I Like Programming In Php Language.

Conclusion

The ucwords() is an in-built PHP function for converting the first letter of every word in a string to uppercase.

The ucfirst() is an in-built PHP function for converting the first letter of the first word in a sentence of a PHP string to uppercase.

If the string is in uppercase, then the above functions won't work. For such a scenario, you have to first convert the strings to lowercase before attempting to use the ucwords() and ucfirst() functions.

To check whether the string is in all uppercase, convert it to uppercase using the strtoupper() function and compare the output with the original string using the double equals operator (==).

It is my hope that this article was helpful to you and that you have managed to fix your problem.