Dear Readers. In this article we will discuss 4 PHP functions to convert case of letter one by one.
- strtoupper
- strtolower
- ucfirst
- ucwords
What is string ?
A string is a collection/sequence of characters. String is one of the data types supported by PHP. The string variables can contain alphanumeric characters. Strings are created when; You declare variable and assign string characters to it. example: $str = “This is string variable”;
Note: only text can be converted: number or special symbol can’t effect anything.
PHP strtoupper() Function
strtoupper() takes one string parameter, and returns that string entirely in uppercase:
syntax
strtoupper("This is uppercase test")
Example 1: strtoupper function in php
<?php
$string = "My name is Pradip Mehta";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted in uppercase text is :<b>".strtoupper($string)."</b>";
?>
![strtoupper function in php](https://1.bp.blogspot.com/-Z-0C6ML2g_g/YHRGJWy8ulI/AAAAAAAAJM0/2fH9HgkahRIvxgKt7OhkphI4tyJqAdahwCNcBGAsYHQ/s1670/string_toupper_example_1.png)
Also, Read
- String reverse without using library function in php
- swap two variables without using 3rd variable
- what is expldoe and implode | explode vs implode
- string replace function in php
Example 2: strtoupper function in php
<?php
$string = "My name is Pradip Mehta @1991";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted in uppercase text is :<b>".strtoupper($string)."</b>";
?>
![strtoupper function in php](https://1.bp.blogspot.com/-Ft1mwzqrfZQ/YHRGdRMPbsI/AAAAAAAAJM8/BRrLH048ehQ1KOMXnnYk0zErjNtIgfzlACNcBGAsYHQ/s1334/string_toupper_example_2.jpg)
PHP strtolower() Function
strtolower() takes one string parameter, and returns that string entirely in lowercase:
syntax
strtolower("This is lowercase test")
<?php
$string = "My name is Pradip Mehta";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted in lowercase text is :<b>".strtolower( $string)."</b>";
?>
![strtolower() function in php](https://1.bp.blogspot.com/-H6XaiqRIm9A/YHRJH7bZ7xI/AAAAAAAAJNE/k_pJ5VLHB_o6BubPT7QI3ooltc0u9Tn-ACNcBGAsYHQ/s1074/string_tolower_example_1.png)
PHP ucfirst() Function
The ucfirst() function converts the first character of a string to uppercase
syntax
<?php
echo ucfirst("this is ucfirst function in php");
?>
<?php
$string = "this is ucfirst function in php";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted :<b>".ucfirst( $string)."</b>";
?>
![ucfirst function in php](https://1.bp.blogspot.com/-S_tZLxMtEkI/YHRLjvr3G6I/AAAAAAAAJNM/2e2yXYt5lbM4I1PBOKx6fXZP6ZdtUt7mACNcBGAsYHQ/s1050/ucfirst.jpg)
PHP ucwords() Function
The ucwords() function converts the first character of each word in a string to uppercase.
syntax
<?php
echo ucwords("this is ucfirst function in php");
?>
<?php
$string = "this is ucfirst function in php";
echo "Origin text is : <b>$string</b>";
echo "<br/><br/> After convernted :<b>".ucwords( $string)."</b>";
?>
![ucwords function in php](https://1.bp.blogspot.com/-Esb7hb-3Kt4/YHRbCDF03bI/AAAAAAAAJNU/B8vyIMavvV0md22v5SI2OekKA4smgcdlwCNcBGAsYHQ/s1040/ucwords.jpg)
Note: If you want to work on with non-Latin UTF-8 text (δρασκελίζει) then you have to use in prefix mb e.g. mb_strtoupper for convert the text into uppercase similarly for other function you can use. for more details click here.