PHP ucfirst() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, ucfirst() makes the first character of a string uppercase.

The rest of the string is left unchanged.

Syntax

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

PHP
$title = ucfirst($word);
CallWhat it does
ucfirst("hello world")Returns "Hello world".
ucfirst("123abc")Returns "123abc" because the first character is a digit.

To capitalize every word, use ucwords(). To lower the first letter, use lcfirst().

Library declarations
PHP
ucfirst(string $string): string

Parameters

ParameterDescription
stringRequired. The input string.

Return Value

Return ValueDescription
stringThe string with the first character converted to uppercase, if it is a letter.

PHP Examples

Example 1 — Capitalize the First Letter

Turn the first letter of a sentence into a capital.

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

echo ucfirst($text), PHP_EOL;

How It Works

  1. Start with lowercase text. Every letter is small.
  2. Call ucfirst(). Only the first character is changed.
  3. Rest unchanged. world keeps its lowercase.
  4. Print. You see Hello world.

Example 2 — When the First Character Is Not a Letter

If the first character is a digit, space, or symbol, nothing changes.

PHP
<?php
echo ucfirst("123abc"), PHP_EOL;
echo "[" . ucfirst(" hi") . "]", PHP_EOL;
echo ucfirst("Already"), PHP_EOL;

How It Works

  1. Digit first. Digits have no uppercase form.
  2. Leading space. The space is the first character, so h stays small.
  3. Already capital. The text is returned the same.
  4. Fix spacing. Use ucfirst(trim($text)) to skip leading spaces.

Example 3 — Normalize a Name

Fix names that were typed with odd capitalization.

PHP
<?php
$name = "jOHN";
$fixed = ucfirst(strtolower($name));

echo $fixed, PHP_EOL;

How It Works

  1. Messy input. jOHN has random capitals.
  2. Lowercase it all. strtolower() gives john.
  3. Capitalize the first letter. ucfirst() makes it John.
  4. Common pattern. Use this order for clean names.

Frequently Asked Questions

ucfirst() converts the first character of a string to uppercase if it is a letter.
string ucfirst(string $string). It takes one string and returns a string.
No. Use ucfirst(strtolower($text)) to make only the first letter capital.
Use ucwords(). For example, ucwords("hello php world") returns "Hello Php World".
Not reliably. It only changes ASCII letters in PHP 8.2 and newer. Use mb_strtoupper(mb_substr(...)) for Unicode.

Next: ucwords()

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

ucwords() 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