In this article, I will show you how to uppercase or lowercase a string in PHP. First of all, let’s see why is it required.
Why are PHP Uppercase & Lowercase functions required?
PHP uppercase or lowercase is required to achieve the following:
- To display data uniformly on webpage no matter how they are stored on back-end.
- To compare the string with another string in case of case insensitive scenario. Example: You have two strings, WEBOLUTE & Webolute. These 2 strings are same but they won’t match in mysql statement having LIKE attribute. To achieve this, you can lowercase both the strings and match them.
PHP Uppercase – strtoupper()
strtoupper( $str )
It converts all the characters of the string to upper-case.
Example:
<?php
$str = "Programming is Interesting";
$str = strtoupper($str);
echo $str; // Output: PROGRAMMING IS INTERESTING
?>
PHP Lowercase – strtolower()
strtolower( $str )
It converts all the characters of the string to lower-case.
Example:
<?php
$str = "Programming is Interesting";
$str = strtolower($str);
echo $str; // Output: programming is interesting
?>
Yes. It is as easy as it looks.