Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
string
uppercase
communitycreator

What is ucword() in PHP?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ucwords() method in PHP makes the initial character of every word in a string uppercase.

The following figure shows a visual representation of the ucwords() function.

Figure 1: Visual representation of ucwords() function

Syntax


ucwords(str, sep)
## where str stands for string and sep stands for separator

Parameter

The ucwords() function requires two parameters:

  • A string
  • A separatorThis is an optional parameter. This character separates the words in a string. Its default value is " ".

Return value

The ucwords() uppercases the initial character of every word in a string.


  • The rest of the string, i.e., other than the first character of every word, stays the same.
  • If the first character of the word is not a letter, then that word of the string remains the same.

Code

The code below shows how the ucwords() function works.

<?php
#string
echo("ucwords('edpresso shots'): ");
echo (ucwords('edpresso shots'));
echo("\n");
#string with number
echo("ucwords('hellO studentS of course 202'): ");
echo (ucwords('hellO studentS of course 202'));
echo("\n");
#string with number along with separator
echo("ucwords('dev,tech,non-tech',','): ");
echo (ucwords('dev,tech,non-tech',','));
?>

RELATED TAGS

php
string
uppercase
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring