PHP ucwords() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, ucwords() makes the first letter of every word uppercase.

By default words are separated by whitespace, but you can choose your own separators.

Syntax

Call ucwords() with the values you need. Start with this line.

PHP
$title = ucwords($text);
CallWhat it does
ucwords("hello world from php")Returns "Hello World From Php".
ucwords("mary-jane watson")Returns "Mary-jane Watson". The hyphen is not a separator by default.
ucwords("mary-jane watson", " -")Returns "Mary-Jane Watson".

ucwords() does not lowercase the other letters. Use strtolower() first when the text is in capitals.

Library declarations
PHP
ucwords(string $string, string $separators = " \t\r\n\f\v"): string

Parameters

ParameterDescription
stringRequired. The input string.
separatorsOptional. Characters that mark the start of a new word. Default is space, tab, carriage return, new line, form feed, and vertical tab.

Return Value

Return ValueDescription
stringThe string with the first letter of each word in uppercase.

PHP Examples

Example 1 — Title Case a Sentence

Capitalize each word in a phrase.

PHP
<?php
$text = "hello world from php";

echo ucwords($text), PHP_EOL;

How It Works

  1. Split by spaces. PHP treats each space as a word break.
  2. Capitalize each start. The first letter of every word is changed.
  3. Keep the rest. Other letters remain lowercase.
  4. Print. You see Hello World From Php.

Example 2 — Custom Separators

Hyphens do not start a new word unless you add them to the separators.

PHP
<?php
$name = "mary-jane watson";

echo ucwords($name), PHP_EOL;
echo ucwords($name, " -"), PHP_EOL;

How It Works

  1. Default separators. Only whitespace starts a new word.
  2. First result. jane stays lowercase.
  3. Add the hyphen. The second argument " -" lists space and hyphen.
  4. Second result. Now Jane is capitalized, too.

Example 3 — Turn a Slug Into a Title

Convert a URL slug into a readable heading.

PHP
<?php
$slug = "php-string-functions";
$title = ucwords(str_replace("-", " ", $slug));

echo $title, PHP_EOL;

How It Works

  1. Start with a slug. Words are joined by hyphens.
  2. Replace hyphens. str_replace() swaps them for spaces.
  3. Capitalize. ucwords() capitalizes each word.
  4. Print. You get Php String Functions.

Frequently Asked Questions

ucwords() converts the first letter of each word in a string to uppercase.
string ucwords(string $string, string $separators = " \t\r\n\f\v"). The separators argument is optional.
It only raises the first letter of each word. Other letters are not lowered. Use ucwords(strtolower($text)) to get "Hello World".
Pass the hyphen as part of the second argument, for example ucwords($text, " -").
ucfirst() changes only the first letter of the whole string. ucwords() changes the first letter of every word.

Next: utf8_decode()

utf8_decode() converts a UTF-8 string to the ISO-8859-1 (Latin-1) encoding.

utf8_decode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful