PHP | ucwords() Function
The ucwords() function is a built-in function in PHP and is used to convert the first character of every word in a string to upper-case.
Syntax:
string ucwords ( $string, $separator )
Parameter: This function accepts two parameters out of which first is compulsory and second is optional. Both of the parameters are explained below:
- $string: This is the input string of which you want to convert the first character of every word to uppercase.
- $separator: This is an optional parameter. This parameter specifies a character which will be used a separator for the words in the input string. For example, if the separator character is ‘|’ and the input string is “Hello|world” then it means that the string contains two words “Hello” and “world”.
Return value: This function returns a string with the first character of every word in uppercase.
Examples:
Input : $str = "Geeks for geeks"
ucwords($str)
Output: Geeks For Geeks
Input : $str = "going BACK he SAW THIS"
ucwords($str)
Output: Going BACK He SAW THIS
Below programs illustrate the ucwords() function in PHP:
Program 1:
<?php // original string $str = "Geeks for geeks"; // string after converting first character // of every word to uppercase $resStr = ucwords($str); print_r($resStr); ?> |
Output:
Geeks For Geeks
Program 2:
<?php // original string $str = "Geeks#for#geeks #PHP #tutorials"; $separator = '#'; // string after converting first character // of every word to uppercase $resStr = ucwords($str, $separator); print_r($resStr); ?> |
Output:
Geeks#For#Geeks #PHP #Tutorials
Note: You should not use the character ‘$’ as a separator because any name in PHP that starts with $ is considered as a variable name. So, your program may give an error that variable not found.
Reference:
http://php.net/manual/en/function.ucwords.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.max() function
- CSS | var() Function
- PHP | Ds\Map xor() Function
- PHP | each() Function
- PHP | pow( ) Function
- PHP | Ds\Map put() Function
- PHP | Ds\Set xor() Function
- p5.js | hex() function
- CSS | rgb() Function
- PHP | min( ) Function
- PHP | key() Function
- PHP | end() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Vaibhav Bajpai



