W3docs

ucwords()

The ucwords() function in PHP is used to capitalize the first character of each word in a string. In this article, we will discuss the ucwords() function in

Introduction

The ucwords() function in PHP capitalizes the first character of each word in a string and returns the result. It's the go-to helper for turning user input like "jane doe" into a presentable "Jane Doe" — handy for names, titles, headings, and labels.

This page covers the syntax, the optional delimiter parameter, the gotchas that surprise most developers (it does not lowercase the rest of the word, and it only splits on whitespace by default), and how ucwords() differs from related functions.

Syntax

ucwords(string $string, string $separators = " \t\r\n\f\v"): string
ParameterDescription
$stringThe input string to transform.
$separators(Optional) The characters that mark the start of a new "word". Defaults to whitespace: space, tab, carriage return, newline, form feed, and vertical tab.

The function returns a new string — it does not modify $string in place, because PHP strings are passed by value.

Basic example

php— editable, runs on the server

Output:

Hello, World!

ucwords() walks the string and uppercases any letter that follows a delimiter (or sits at the very start). Here both hello and world get their first letter capitalized.

Gotcha: it does not lowercase the rest

A common surprise is that ucwords() only touches the first letter of each word. Letters already in uppercase are left untouched:

<?php

echo ucwords("hello WORLD"); // Hello WORLD — "WORLD" keeps its existing uppercase letters
echo "\n";
echo ucwords("MARY had a LITTLE lamb"); // MARY Had A LITTLE Lamb

If you want a clean title where only the first letter of each word is uppercase, lowercase the string first with strtolower():

<?php

$title = "MARY HAD A LITTLE LAMB";
echo ucwords(strtolower($title)); // Mary Had A Little Lamb

Custom delimiters

By default only whitespace separates words, so hyphenated or piped values are treated as a single word. Pass the optional $separators argument to capitalize after other characters too:

<?php

// Default: only the first letter is capitalized.
echo ucwords("hello-world|of-php");       // Hello-world|of-php
echo "\n";

// Treat "-" and "|" as word boundaries as well.
echo ucwords("hello-world|of-php", "-|"); // Hello-World|Of-Php

This is useful for slugs, names like "o'brien", or any token-separated text. Note that when you supply $separators, whitespace is no longer a delimiter unless you include it yourself — so a typical call is ucwords($name, " -").

FunctionWhat it does
ucfirst()Capitalizes only the first character of the whole string.
ucwords()Capitalizes the first character of every word.
strtoupper()Converts the entire string to uppercase.
strtolower()Converts the entire string to lowercase.
lcfirst()Lowercases the first character of the string.

Note on multibyte and accented characters

ucwords() works byte-by-byte and is reliable for plain ASCII. It does not correctly capitalize accented or non-Latin letters (for example é, ñ, or Cyrillic). For Unicode-aware title casing, use the mbstring extension with mb_convert_case($string, MB_CASE_TITLE, "UTF-8") instead.

Conclusion

ucwords() is the standard way to capitalize the first letter of each word in a string. Remember its two key behaviors: it leaves the remaining letters as-is (combine it with strtolower() for clean title case), and it splits on whitespace unless you pass custom $separators. For accented text, reach for mb_convert_case() instead.

Practice

Practice
What is the function of the 'ucwords' function in PHP?
What is the function of the 'ucwords' function in PHP?
Was this page helpful?