Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท String Functions

PHP ucwords() function

Reference for PHP ucwords(): uppercase the first letter of each word, change the delimiter list for hyphens and underscores, and handle mixed-case input.

ucwords() returns a string in which the first letter of every word is uppercased. By default a "word" starts after a space, a tab, a carriage return, a newline, a form feed or a vertical tab. The remaining letters are not changed.

You can pass your own list of delimiter characters, which makes it useful for hyphenated names or snake_case identifiers.

PHP
<?php
echo ucwords("the quick brown fox");  // The Quick Brown Fox

Try it

PHP Default behavior

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Custom delimiters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
ucwords(string $string, string $separators = " \t\r\n\f\v"): string
Name Description
$string The input string.
$separators Optional. Every character in this string counts as a word separator. The character after a separator is uppercased.

Return value: the string with the first letter of each word uppercased (ASCII letters only).

Usage notes

  • Only the first letter of each word is changed. Combine with strtolower() to turn "jOHN sMITH" into John Smith.
  • Custom $separators replace the default list. If you pass "-", spaces no longer separate words; pass " -" to get both.
  • Hyphens and underscores are not separators by default, which is why "hello-world" stays Hello-world.
  • ASCII only. Since PHP 8.2 the function no longer depends on the locale. For Unicode text use mb_convert_case($s, MB_CASE_TITLE).
  • Not real title case. Small words ("of", "the") are capitalized too, and names such as McDonald are not handled.
  • Related single-letter functions: ucfirst() for the start of the string only, lcfirst() for the opposite.