0
0
PHPprogramming~5 mins

Case conversion functions in PHP

Choose your learning style9 modes available
Introduction

Case conversion functions help change text to uppercase or lowercase. This makes text easier to compare or display consistently.

When you want to make user input all lowercase to compare it fairly.
When displaying names or titles in uppercase for emphasis.
When normalizing email addresses to lowercase before saving.
When formatting text to a consistent style for reports.
Syntax
PHP
$upper = strtoupper($text);
$lower = strtolower($text);
$ucfirst = ucfirst($text);
$ucwords = ucwords($text);

strtoupper() converts all letters to uppercase.

strtolower() converts all letters to lowercase.

ucfirst() makes only the first letter uppercase.

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

Examples
This changes all letters to uppercase: HELLO WORLD
PHP
$text = "hello world";
echo strtoupper($text);
This changes all letters to lowercase: hello world
PHP
$text = "HELLO WORLD";
echo strtolower($text);
This makes only the first letter uppercase: Hello world
PHP
$text = "hello world";
echo ucfirst($text);
This makes the first letter of each word uppercase: Hello World
PHP
$text = "hello world";
echo ucwords($text);
Sample Program

This program shows how to convert the same text using different case functions.

PHP
<?php
$text = "php is fun";

echo "Original: $text\n";
echo "Uppercase: " . strtoupper($text) . "\n";
echo "Lowercase: " . strtolower("PHP IS FUN") . "\n";
echo "First letter uppercase: " . ucfirst($text) . "\n";
echo "First letter of each word uppercase: " . ucwords($text) . "\n";
?>
OutputSuccess
Important Notes

These functions only affect letters; numbers and symbols stay the same.

Use these functions to make text comparisons easier and consistent.

Summary

Use strtoupper() to make all letters uppercase.

Use strtolower() to make all letters lowercase.

Use ucfirst() and ucwords() to capitalize first letters.