PHP Capitalize String Functions - Change the case of a string

With PHP you can perform a number of capitalization or case-changing operations on strings. You can capitalize all the letters in a string, the first letters of words only, or even the first letter of a string only.

You can also convert parts of a string to lowercase if that is what you need to do.

The PHP capitalization tutorial below will help to get you familiar with the various capitalization and de-capitalization operations.

PHP Capitalize the first character of a string

To capitalize the first character of a string (in other words: make it uppercase), we use the ucfirst function. Look at the code below:

<?php
 
$string = "i love tutorial arena so much";
 
$result = ucfirst($string);
 
echo $string;
echo '<br />';
echo $result;
 
?>

The output is:

i love tutorial arena so much
I love tutorial arena so much

As we can see, the first character of the string has been capitalized.

PHP Capitalize the first character of each word in a string

To capitalize the first character of each word in a string, we use the ucwords function. Look at the code below:

<?php
 
$string = "i love tutorial arena so much";
 
$result = ucwords($string);
 
echo $string;
echo '<br />';
echo $result;
 
?>

The output is:

i love tutorial arena so much
I Love Tutorial Arena So Much

As we can see, the first character of each word in the string has been capitalized.

PHP Capitalize an entire string - Convert it to uppercase

To capitalize an entire string, we use the strtoupper function. Look at the code below:

<?php
 
$string = "i love tutorial arena so much";
 
$result = strtoupper($string);
 
echo $string;
echo '<br />';
echo $result;
 
?>

The output is:

i love tutorial arena so much
I LOVE TUTORIAL ARENA SO MUCH

As we can see, the entire string has been capitalized.

Convert an entire string to lowercase

We use the strtolower function to make an entire string lowercase. Look at the code below:

<?php
 
$string = "I LoVe tUtoRIAl ARENA SO MUCH";
 
$result = strtolower($string);
 
echo $string;
echo '<br />';
echo $result;
 
?>

The output is:

I LoVe tUtoRIAl ARENA SO MUCH
i love tutorial arena so much

As we can see, the entire string has been converted to lowercase.

Whether you want to change strings to uppercase, to lowercase, or even capitalize every word in a string, it is easily accomplished using PHP capitalization functions. We hope this PHP tutorial was helpful.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...