PHP String ucwords() Function
The ucwords() is an in-built function of PHP, which is used to convert the first character of each word to uppercase in a string. The ucwords() is supported by the PHP 4 and above versions. It takes a string as an input and converts the first character of each word of the string to uppercase. The other characters of the string remain the same.
Note: The ucwords() is a binary-safe function.
There are some other functions in PHP which are similar to the ucwords() function:
The syntax of the ucwords() function is given below that accepts two parameters.
The ucwords() returns the converted string whose first character of each word is converted to uppercase.
$string (required) - It is a mandatory parameter of this function, which specifies the input string that needs to be converted.
$separator (optional) - It is an optional parameter of this function, which contains the words separator characters. It specifies a character that uses a separator for the words in the input string. By default these separator characters are:
The ucwords() function returns the modified string, where the first character of each word in a string is converted to uppercase.
There are some examples given, through which we can learn the working of the ucwords() function. Let's see the below examples-
Hello, My Name Is Lovyansh.
Before: Good morning! everyone. After: Good Morning! Everyone.
In the above example, we have used "|" as a separator, which needs to be passed in ucwords() while modifying the string.
Note: Doller $ symbol cannot be used as a separator, because $ is used before every variable in PHP. So, the program will generate an error "variable not found."