Converting PHP Strings between Upper and Lowercase

Learn how to use PHP to convert text between uppercase and lowercase. Looks at strtolower(), strtoupper(), lcfirst(), ucfirst(), ucwords(), and more.

One of the nice things about working with strings in PHP is its huge range of built-in string-handling functions. If you want to do something to a string, chances are there's a PHP function to do it for you!

In this article you'll look at PHP's string functions for converting text between uppercase and lowercase:

  • strtolower() for converting a string to lowercase
  • strtoupper() for converting a string to uppercase
  • lcfirst() for converting the first letter to lowercase
  • ucfirst() for converting the first letter to uppercase
  • ucwords() for converting each word to uppercase

Converting a whole string to lowercase with strtolower()

strtolower() takes a string of text, and returns a copy of the string with all letters converted to lowercase:


$myString = 'Hello there! How are you?';
echo strtolower( $myString ); // Displays "hello there! how are you?"

strtolower() is handy if you want to ensure that automatically-generated URLs, filenames, or passwords are all in lowercase. It's also useful for comparing 2 strings regardless of their case (for example, "Hello" and "hello") &mdash just convert both strings using strtolower() before comparing them:


$myString1 = 'Hello!';
$myString2 = 'hello!';

// Displays "Strings match"
if ( strtolower( $myString1 ) == strtolower( $myString2 ) ) echo "Strings match";

If you need to convert a string containing Unicode characters to lowercase, use mb_strtolower() instead.

Converting a whole string to uppercase with strtoupper()

strtoupper() does the opposite of strtolower(), converting an entire string to uppercase letters:


$myString = 'Hello there! How are you?';
echo strtoupper( $myString ); // Displays "HELLO THERE! HOW ARE YOU?"

If you need to convert a string containing Unicode characters to uppercase, use mb_strtoupper() instead.

Converting the first letter to lowercase with lcfirst()

lcfirst() converts just the first letter of a string to lowercase:


$myString = 'Hello there! How are you?';
echo lcfirst( $myString ); // Displays "hello there! How are you?"

lcfirst() was introduced in PHP 5.3.

Converting the first letter to uppercase with ucfirst()

ucfirst() — the counterpart to lcfirst() — converts just the first letter of a string to uppercase:


$myString = 'hello there! how are you?';
echo ucfirst( $myString ); // Displays "Hello there! how are you?"

Converting the first letter of each word to uppercase with ucwords()

The ucwords() function converts the first letter of each word in a string to uppercase — great for formatting page titles and so on:


$myString = 'Hello there! How are you?';
echo ucwords( $myString ); // Displays "Hello There! How Are You?"

As far as ucwords() is concerned, a "word" is any string of letters that follows whitespace (or that is at the start of the string).

If you want to convert a Unicode string, use mb_convert_case() with the MB_CASE_TITLE mode instead.

You now know how to convert case in PHP. You've looked at strtolower() and strtoupper() for converting whole strings; lcfirst() and ucfirst() for converting just the first letter of a string; and ucwords() for converting the first letter of each word to uppercase.

Happy coding! If you have any questions on converting the case of PHP strings, feel free to post a response below.

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or Amazon.co.ukBeginning PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page