• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Strings / Converting PHP Strings between Upper and Lowercase

Converting PHP Strings between Upper and Lowercase

29 October 2009 / Leave a Comment

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.

Filed Under: PHP Strings Tagged With: convert case, lcfirst, mb_convert_case, mb_strtolower, mb_strtoupper, php strings, strtolower, strtoupper, ucfirst, ucwords

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits